PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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, Cardinality numGroups, 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 setup_eager_aggregation (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 *distinct_cols)
 
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 
)
extern

Definition at line 178 of file initsplan.c.

179{
180 if (jtnode == NULL)
181 return;
182 if (IsA(jtnode, RangeTblRef))
183 {
184 int varno = ((RangeTblRef *) jtnode)->rtindex;
185
186 (void) build_simple_rel(root, varno, NULL);
187 }
188 else if (IsA(jtnode, FromExpr))
189 {
190 FromExpr *f = (FromExpr *) jtnode;
191 ListCell *l;
192
193 foreach(l, f->fromlist)
195 }
196 else if (IsA(jtnode, JoinExpr))
197 {
198 JoinExpr *j = (JoinExpr *) jtnode;
199
202 }
203 else
204 elog(ERROR, "unrecognized node type: %d",
205 (int) nodeTag(jtnode));
206}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition initsplan.c:178
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
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition relnode.c:212
List * fromlist
Definition primnodes.h:2396

References add_base_rels_to_query(), build_simple_rel(), elog, ERROR, fb(), FromExpr::fromlist, IsA, j, 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)
extern

Definition at line 216 of file initsplan.c.

217{
218 int rti;
219
220 for (rti = 1; rti < root->simple_rel_array_size; rti++)
221 {
222 RelOptInfo *rel = root->simple_rel_array[rti];
223 RangeTblEntry *rte = root->simple_rte_array[rti];
224
225 /* there may be empty slots corresponding to non-baserel RTEs */
226 if (rel == NULL)
227 continue;
228
229 /* Ignore any "otherrels" that were already added. */
230 if (rel->reloptkind != RELOPT_BASEREL)
231 continue;
232
233 /* If it's marked as inheritable, look for children. */
234 if (rte->inh)
236 }
237}
void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte, Index rti)
Definition inherit.c:88
@ RELOPT_BASEREL
Definition pathnodes.h:977
RelOptKind reloptkind
Definition pathnodes.h:1015

References expand_inherited_rtentry(), fb(), 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 
)
extern

Definition at line 373 of file initsplan.c.

375{
376 ListCell *temp;
377
379
380 foreach(temp, vars)
381 {
382 Node *node = (Node *) lfirst(temp);
383
384 if (IsA(node, Var))
385 {
386 Var *var = (Var *) node;
387 RelOptInfo *rel = find_base_rel(root, var->varno);
388 int attno = var->varattno;
389
391 continue;
392 Assert(attno >= rel->min_attr && attno <= rel->max_attr);
393 attno -= rel->min_attr;
394 rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
396 }
397 else if (IsA(node, PlaceHolderVar))
398 {
401
402 phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
404 }
405 else
406 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
407 }
408}
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:901
#define bms_is_empty(a)
Definition bitmapset.h:118
#define Assert(condition)
Definition c.h:943
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
Definition placeholder.c:85
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:544
Definition nodes.h:135
Relids relids
Definition pathnodes.h:1021
AttrNumber min_attr
Definition pathnodes.h:1075
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270

References Assert, bms_add_members(), bms_is_empty, bms_is_subset(), elog, ERROR, fb(), find_base_rel(), find_placeholder_info(), IsA, lfirst, RelOptInfo::min_attr, nodeTag, 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 
)
extern

Definition at line 302 of file initsplan.c.

304{
305 ListCell *temp;
306
308
309 foreach(temp, vars)
310 {
311 Node *node = (Node *) lfirst(temp);
312
313 if (IsA(node, Var))
314 {
315 Var *var = (Var *) node;
316 RelOptInfo *rel = find_base_rel(root, var->varno);
317 int attno = var->varattno;
318
320 continue;
321 Assert(attno >= rel->min_attr && attno <= rel->max_attr);
322 attno -= rel->min_attr;
323 if (rel->attr_needed[attno] == NULL)
324 {
325 /*
326 * Variable not yet requested, so add to rel's targetlist.
327 *
328 * The value available at the rel's scan level has not been
329 * nulled by any outer join, so drop its varnullingrels.
330 * (We'll put those back as we climb up the join tree.)
331 */
332 var = copyObject(var);
333 var->varnullingrels = NULL;
334 rel->reltarget->exprs = lappend(rel->reltarget->exprs, var);
335 /* reltarget cost and width will be computed later */
336 }
337 rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
339 }
340 else if (IsA(node, PlaceHolderVar))
341 {
344
345 phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
347 }
348 else
349 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
350 }
351}
List * lappend(List *list, void *datum)
Definition list.c:339
#define copyObject(obj)
Definition nodes.h:232
List * exprs
Definition pathnodes.h:1878
struct PathTarget * reltarget
Definition pathnodes.h:1045

References Assert, bms_add_members(), bms_is_empty, bms_is_subset(), copyObject, elog, ERROR, PathTarget::exprs, fb(), find_base_rel(), find_placeholder_info(), IsA, lappend(), lfirst, RelOptInfo::min_attr, nodeTag, 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 
)
extern

Definition at line 255 of file initsplan.c.

256{
257 List *tlist_vars = pull_var_clause((Node *) final_tlist,
261
262 if (tlist_vars != NIL)
263 {
265 list_free(tlist_vars);
266 }
267
268 /*
269 * If there's a HAVING clause, we'll need the Vars it uses, too. Note
270 * that HAVING can contain Aggrefs but not WindowFuncs.
271 */
272 if (root->parse->havingQual)
273 {
274 List *having_vars = pull_var_clause(root->parse->havingQual,
277
278 if (having_vars != NIL)
279 {
283 }
284 }
285}
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:302
void list_free(List *list)
Definition list.c:1546
#define PVC_RECURSE_AGGREGATES
Definition optimizer.h:198
#define PVC_RECURSE_WINDOWFUNCS
Definition optimizer.h:200
#define PVC_INCLUDE_PLACEHOLDERS
Definition optimizer.h:201
#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(), fb(), 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 
)
extern

Definition at line 3858 of file initsplan.c.

3865{
3867 Expr *clause;
3868
3869 /*
3870 * Build the new clause. Copy to ensure it shares no substructure with
3871 * original (this is necessary in case there are subselects in there...)
3872 */
3873 clause = make_opclause(opno,
3874 BOOLOID, /* opresulttype */
3875 false, /* opretset */
3878 InvalidOid,
3879 collation);
3880
3881 /*
3882 * Build the RestrictInfo node itself.
3883 */
3885 clause,
3886 true, /* is_pushed_down */
3887 false, /* !has_clone */
3888 false, /* !is_clone */
3889 false, /* pseudoconstant */
3890 security_level, /* security_level */
3891 qualscope, /* required_relids */
3892 NULL, /* incompatible_relids */
3893 NULL); /* outer_relids */
3894
3895 /* Set mergejoinability/hashjoinability flags */
3899
3900 return restrictinfo;
3901}
static void check_hashjoinable(RestrictInfo *restrictinfo)
Definition initsplan.c:4235
static void check_mergejoinable(RestrictInfo *restrictinfo)
Definition initsplan.c:4198
static void check_memoizable(RestrictInfo *restrictinfo)
Definition initsplan.c:4263
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition makefuncs.c:701
#define InvalidOid
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)

References check_hashjoinable(), check_memoizable(), check_mergejoinable(), copyObject, fb(), InvalidOid, make_opclause(), make_restrictinfo(), 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 
)
extern

Definition at line 1992 of file createplan.c.

1993{
1994 /*
1995 * If the top plan node can't do projections and its existing target list
1996 * isn't already what we need, we need to add a Result node to help it
1997 * along.
1998 */
1999 if (!is_projection_capable_plan(subplan) &&
2000 !tlist_same_exprs(tlist, subplan->targetlist))
2001 subplan = inject_projection_plan(subplan, tlist,
2002 subplan->parallel_safe &&
2004 else
2005 {
2006 /* Else we can just replace the plan node's tlist */
2007 subplan->targetlist = tlist;
2009 }
2010 return subplan;
2011}
bool is_projection_capable_plan(Plan *plan)
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
bool parallel_safe
Definition plannodes.h:221
List * targetlist
Definition plannodes.h:235
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition tlist.c:227

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

Referenced by create_nestloop_plan(), and postgresGetForeignPlan().

◆ create_lateral_join_info()

void create_lateral_join_info ( PlannerInfo root)
extern

Definition at line 1283 of file initsplan.c.

1284{
1285 bool found_laterals = false;
1286 Index rti;
1287 ListCell *lc;
1288
1289 /* We need do nothing if the query contains no LATERAL RTEs */
1290 if (!root->hasLateralRTEs)
1291 return;
1292
1293 /* We'll need to have the ph_eval_at values for PlaceHolderVars */
1294 Assert(root->placeholdersFrozen);
1295
1296 /*
1297 * Examine all baserels (the rel array has been set up by now).
1298 */
1299 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1300 {
1301 RelOptInfo *brel = root->simple_rel_array[rti];
1302 Relids lateral_relids;
1303
1304 /* there may be empty slots corresponding to non-baserel RTEs */
1305 if (brel == NULL)
1306 continue;
1307
1308 Assert(brel->relid == rti); /* sanity check on array */
1309
1310 /* ignore RTEs that are "other rels" */
1311 if (brel->reloptkind != RELOPT_BASEREL)
1312 continue;
1313
1314 lateral_relids = NULL;
1315
1316 /* consider each laterally-referenced Var or PHV */
1317 foreach(lc, brel->lateral_vars)
1318 {
1319 Node *node = (Node *) lfirst(lc);
1320
1321 if (IsA(node, Var))
1322 {
1323 Var *var = (Var *) node;
1324
1325 found_laterals = true;
1326 lateral_relids = bms_add_member(lateral_relids,
1327 var->varno);
1328 }
1329 else if (IsA(node, PlaceHolderVar))
1330 {
1331 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1333
1334 found_laterals = true;
1335 lateral_relids = bms_add_members(lateral_relids,
1336 phinfo->ph_eval_at);
1337 }
1338 else
1339 Assert(false);
1340 }
1341
1342 /* We now have all the simple lateral refs from this rel */
1343 brel->direct_lateral_relids = lateral_relids;
1344 brel->lateral_relids = bms_copy(lateral_relids);
1345 }
1346
1347 /*
1348 * Now check for lateral references within PlaceHolderVars, and mark their
1349 * eval_at rels as having lateral references to the source rels.
1350 *
1351 * For a PHV that is due to be evaluated at a baserel, mark its source(s)
1352 * as direct lateral dependencies of the baserel (adding onto the ones
1353 * recorded above). If it's due to be evaluated at a join, mark its
1354 * source(s) as indirect lateral dependencies of each baserel in the join,
1355 * ie put them into lateral_relids but not direct_lateral_relids. This is
1356 * appropriate because we can't put any such baserel on the outside of a
1357 * join to one of the PHV's lateral dependencies, but on the other hand we
1358 * also can't yet join it directly to the dependency.
1359 */
1360 foreach(lc, root->placeholder_list)
1361 {
1363 Relids eval_at = phinfo->ph_eval_at;
1365 int varno;
1366
1367 if (phinfo->ph_lateral == NULL)
1368 continue; /* PHV is uninteresting if no lateral refs */
1369
1370 found_laterals = true;
1371
1372 /*
1373 * Include only baserels not outer joins in the evaluation sites'
1374 * lateral relids. This avoids problems when outer join order gets
1375 * rearranged, and it should still ensure that the lateral values are
1376 * available when needed.
1377 */
1378 lateral_refs = bms_intersect(phinfo->ph_lateral, root->all_baserels);
1380
1381 if (bms_get_singleton_member(eval_at, &varno))
1382 {
1383 /* Evaluation site is a baserel */
1384 RelOptInfo *brel = find_base_rel(root, varno);
1385
1386 brel->direct_lateral_relids =
1387 bms_add_members(brel->direct_lateral_relids,
1388 lateral_refs);
1389 brel->lateral_relids =
1390 bms_add_members(brel->lateral_relids,
1391 lateral_refs);
1392 }
1393 else
1394 {
1395 /* Evaluation site is a join */
1396 varno = -1;
1397 while ((varno = bms_next_member(eval_at, varno)) >= 0)
1398 {
1400
1401 if (brel == NULL)
1402 continue; /* ignore outer joins in eval_at */
1403 brel->lateral_relids = bms_add_members(brel->lateral_relids,
1404 lateral_refs);
1405 }
1406 }
1407 }
1408
1409 /*
1410 * If we found no actual lateral references, we're done; but reset the
1411 * hasLateralRTEs flag to avoid useless work later.
1412 */
1413 if (!found_laterals)
1414 {
1415 root->hasLateralRTEs = false;
1416 return;
1417 }
1418
1419 /*
1420 * Calculate the transitive closure of the lateral_relids sets, so that
1421 * they describe both direct and indirect lateral references. If relation
1422 * X references Y laterally, and Y references Z laterally, then we will
1423 * have to scan X on the inside of a nestloop with Z, so for all intents
1424 * and purposes X is laterally dependent on Z too.
1425 *
1426 * This code is essentially Warshall's algorithm for transitive closure.
1427 * The outer loop considers each baserel, and propagates its lateral
1428 * dependencies to those baserels that have a lateral dependency on it.
1429 */
1430 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1431 {
1432 RelOptInfo *brel = root->simple_rel_array[rti];
1434 Index rti2;
1435
1436 if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
1437 continue;
1438
1439 /* need not consider baserel further if it has no lateral refs */
1440 outer_lateral_relids = brel->lateral_relids;
1442 continue;
1443
1444 /* else scan all baserels */
1445 for (rti2 = 1; rti2 < root->simple_rel_array_size; rti2++)
1446 {
1447 RelOptInfo *brel2 = root->simple_rel_array[rti2];
1448
1449 if (brel2 == NULL || brel2->reloptkind != RELOPT_BASEREL)
1450 continue;
1451
1452 /* if brel2 has lateral ref to brel, propagate brel's refs */
1453 if (bms_is_member(rti, brel2->lateral_relids))
1454 brel2->lateral_relids = bms_add_members(brel2->lateral_relids,
1456 }
1457 }
1458
1459 /*
1460 * Now that we've identified all lateral references, mark each baserel
1461 * with the set of relids of rels that reference it laterally (possibly
1462 * indirectly) --- that is, the inverse mapping of lateral_relids.
1463 */
1464 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1465 {
1466 RelOptInfo *brel = root->simple_rel_array[rti];
1467 Relids lateral_relids;
1468 int rti2;
1469
1470 if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
1471 continue;
1472
1473 /* Nothing to do at rels with no lateral refs */
1474 lateral_relids = brel->lateral_relids;
1475 if (bms_is_empty(lateral_relids))
1476 continue;
1477
1478 /* No rel should have a lateral dependency on itself */
1479 Assert(!bms_is_member(rti, lateral_relids));
1480
1481 /* Mark this rel's referencees */
1482 rti2 = -1;
1483 while ((rti2 = bms_next_member(lateral_relids, rti2)) >= 0)
1484 {
1485 RelOptInfo *brel2 = root->simple_rel_array[rti2];
1486
1487 if (brel2 == NULL)
1488 continue; /* must be an OJ */
1489
1490 Assert(brel2->reloptkind == RELOPT_BASEREL);
1491 brel2->lateral_referencers =
1492 bms_add_member(brel2->lateral_referencers, rti);
1493 }
1494 }
1495}
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:1290
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition bitmapset.c:708
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
unsigned int Index
Definition c.h:698
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition relnode.c:584
Relids lateral_relids
Definition pathnodes.h:1064

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(), fb(), find_base_rel(), find_base_rel_ignore_join(), find_placeholder_info(), IsA, RelOptInfo::lateral_relids, lfirst, RELOPT_BASEREL, root, and Var::varno.

Referenced by query_planner().

◆ create_plan()

Plan * create_plan ( PlannerInfo root,
Path best_path 
)
extern

Definition at line 339 of file createplan.c.

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

References apply_tlist_labeling(), Assert, CP_EXACT_TLIST, create_plan_recurse(), elog, ERROR, fb(), 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)
extern

Definition at line 1522 of file initsplan.c.

1523{
1524 List *result;
1526 List *item_list = NIL;
1527 ListCell *lc;
1528
1529 /*
1530 * After this point, no more PlaceHolderInfos may be made, because
1531 * make_outerjoininfo requires all active placeholders to be present in
1532 * root->placeholder_list while we crawl up the join tree.
1533 */
1534 root->placeholdersFrozen = true;
1535
1536 /* Fetch the already-created top-level join domain for the query */
1537 top_jdomain = linitial_node(JoinDomain, root->join_domains);
1538 top_jdomain->jd_relids = NULL; /* filled during deconstruct_recurse */
1539
1540 /* Start recursion at top of jointree */
1541 Assert(root->parse->jointree != NULL &&
1542 IsA(root->parse->jointree, FromExpr));
1543
1544 /* These are filled as we scan the jointree */
1545 root->all_baserels = NULL;
1546 root->outer_join_rels = NULL;
1547
1548 /* Perform the initial scan of the jointree */
1549 result = deconstruct_recurse(root, (Node *) root->parse->jointree,
1551 &item_list);
1552
1553 /* Now we can form the value of all_query_rels, too */
1554 root->all_query_rels = bms_union(root->all_baserels, root->outer_join_rels);
1555
1556 /* ... which should match what we computed for the top join domain */
1557 Assert(bms_equal(root->all_query_rels, top_jdomain->jd_relids));
1558
1559 /* Now scan all the jointree nodes again, and distribute quals */
1560 foreach(lc, item_list)
1561 {
1563
1565 }
1566
1567 /*
1568 * If there were any special joins then we may have some postponed LEFT
1569 * JOIN clauses to deal with.
1570 */
1571 if (root->join_info_list)
1572 {
1573 foreach(lc, item_list)
1574 {
1576
1577 if (jtitem->oj_joinclauses != NIL)
1579 }
1580 }
1581
1582 /* Don't need the JoinTreeItems any more */
1584
1585 return result;
1586}
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
uint32 result
static List * deconstruct_recurse(PlannerInfo *root, Node *jtnode, JoinDomain *parent_domain, JoinTreeItem *parent_jtitem, List **item_list)
Definition initsplan.c:1604
static void deconstruct_distribute_oj_quals(PlannerInfo *root, List *jtitems, JoinTreeItem *jtitem)
Definition initsplan.c:2664
static void deconstruct_distribute(PlannerInfo *root, JoinTreeItem *jtitem)
Definition initsplan.c:1902
void list_free_deep(List *list)
Definition list.c:1560
#define linitial_node(type, l)
Definition pg_list.h:181

References Assert, bms_equal(), bms_union(), deconstruct_distribute(), deconstruct_distribute_oj_quals(), deconstruct_recurse(), fb(), IsA, lfirst, linitial_node, list_free_deep(), NIL, result, and root.

Referenced by query_planner().

◆ distribute_restrictinfo_to_rels()

void distribute_restrictinfo_to_rels ( PlannerInfo root,
RestrictInfo restrictinfo 
)
extern

Definition at line 3629 of file initsplan.c.

3631{
3632 Relids relids = restrictinfo->required_relids;
3633
3634 if (!bms_is_empty(relids))
3635 {
3636 int relid;
3637
3638 if (bms_get_singleton_member(relids, &relid))
3639 {
3640 /*
3641 * There is only one relation participating in the clause, so it
3642 * is a restriction clause for that relation.
3643 */
3645 }
3646 else
3647 {
3648 /*
3649 * The clause is a join clause, since there is more than one rel
3650 * in its relid set.
3651 */
3652
3653 /*
3654 * Check for hashjoinable operators. (We don't bother setting the
3655 * hashjoin info except in true join clauses.)
3656 */
3658
3659 /*
3660 * Likewise, check if the clause is suitable to be used with a
3661 * Memoize node to cache inner tuples during a parameterized
3662 * nested loop.
3663 */
3665
3666 /*
3667 * Add clause to the join lists of all the relevant relations.
3668 */
3670 }
3671 }
3672 else
3673 {
3674 /*
3675 * clause references no rels, and therefore we have no place to attach
3676 * it. Shouldn't get here if callers are working properly.
3677 */
3678 elog(ERROR, "cannot cope with variable-free clause");
3679 }
3680}
static void add_base_clause_to_rel(PlannerInfo *root, Index relid, RestrictInfo *restrictinfo)
Definition initsplan.c:3418
void add_join_clause_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition joininfo.c:98

References add_base_clause_to_rel(), add_join_clause_to_rels(), bms_get_singleton_member(), bms_is_empty, check_hashjoinable(), check_memoizable(), elog, ERROR, fb(), 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 
)
extern

Definition at line 3749 of file setrefs.c.

3750{
3751 if (node == NULL)
3752 return false;
3753 Assert(!IsA(node, PlaceHolderVar));
3754 if (IsA(node, Query))
3755 {
3756 Query *query = (Query *) node;
3757 ListCell *lc;
3758
3759 if (query->commandType == CMD_UTILITY)
3760 {
3761 /*
3762 * This logic must handle any utility command for which parse
3763 * analysis was nontrivial (cf. stmt_requires_parse_analysis).
3764 *
3765 * Notably, CALL requires its own processing.
3766 */
3767 if (IsA(query->utilityStmt, CallStmt))
3768 {
3769 CallStmt *callstmt = (CallStmt *) query->utilityStmt;
3770
3771 /* We need not examine funccall, just the transformed exprs */
3773 context);
3775 context);
3776 return false;
3777 }
3778
3779 /*
3780 * Ignore other utility statements, except those (such as EXPLAIN)
3781 * that contain a parsed-but-not-planned query. For those, we
3782 * just need to transfer our attention to the contained query.
3783 */
3784 query = UtilityContainsQuery(query->utilityStmt);
3785 if (query == NULL)
3786 return false;
3787 }
3788
3789 /* Remember if any Query has RLS quals applied by rewriter */
3790 if (query->hasRowSecurity)
3791 context->glob->dependsOnRole = true;
3792
3793 /* Collect relation OIDs in this Query's rtable */
3794 foreach(lc, query->rtable)
3795 {
3797
3798 if (rte->rtekind == RTE_RELATION ||
3799 (rte->rtekind == RTE_SUBQUERY && OidIsValid(rte->relid)) ||
3800 (rte->rtekind == RTE_NAMEDTUPLESTORE && OidIsValid(rte->relid)))
3801 context->glob->relationOids =
3802 lappend_oid(context->glob->relationOids, rte->relid);
3803 }
3804
3805 /* And recurse into the query's subexpressions */
3807 context, 0);
3808 }
3809 /* Extract function dependencies and check for regclass Consts */
3810 fix_expr_common(context, node);
3812 context);
3813}
#define OidIsValid(objectId)
Definition c.h:858
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:280
@ RTE_NAMEDTUPLESTORE
@ RTE_SUBQUERY
@ RTE_RELATION
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition setrefs.c:2102
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition setrefs.c:3749
bool dependsOnRole
Definition pathnodes.h:251
List * relationOids
Definition pathnodes.h:227
PlannerGlobal * glob
Definition pathnodes.h:312
List * rtable
Definition parsenodes.h:178
CmdType commandType
Definition parsenodes.h:121
Node * utilityStmt
Definition parsenodes.h:141
Query * UtilityContainsQuery(Node *parsetree)
Definition utility.c:2199

References Assert, CMD_UTILITY, Query::commandType, PlannerGlobal::dependsOnRole, expression_tree_walker, extract_query_dependencies_walker(), fb(), fix_expr_common(), PlannerInfo::glob, IsA, lappend_oid(), lfirst, OidIsValid, query_tree_walker, PlannerGlobal::relationOids, Query::rtable, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, 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)
extern

Definition at line 1096 of file initsplan.c.

1097{
1098 Index rti;
1099
1100 /* We need do nothing if the query contains no LATERAL RTEs */
1101 if (!root->hasLateralRTEs)
1102 return;
1103
1104 /*
1105 * Examine all baserels (the rel array has been set up by now).
1106 */
1107 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1108 {
1109 RelOptInfo *brel = root->simple_rel_array[rti];
1110
1111 /* there may be empty slots corresponding to non-baserel RTEs */
1112 if (brel == NULL)
1113 continue;
1114
1115 Assert(brel->relid == rti); /* sanity check on array */
1116
1117 /*
1118 * This bit is less obvious than it might look. We ignore appendrel
1119 * otherrels and consider only their parent baserels. In a case where
1120 * a LATERAL-containing UNION ALL subquery was pulled up, it is the
1121 * otherrel that is actually going to be in the plan. However, we
1122 * want to mark all its lateral references as needed by the parent,
1123 * because it is the parent's relid that will be used for join
1124 * planning purposes. And the parent's RTE will contain all the
1125 * lateral references we need to know, since the pulled-up member is
1126 * nothing but a copy of parts of the original RTE's subquery. We
1127 * could visit the parent's children instead and transform their
1128 * references back to the parent's relid, but it would be much more
1129 * complicated for no real gain. (Important here is that the child
1130 * members have not yet received any processing beyond being pulled
1131 * up.) Similarly, in appendrels created by inheritance expansion,
1132 * it's sufficient to look at the parent relation.
1133 */
1134
1135 /* ignore RTEs that are "other rels" */
1136 if (brel->reloptkind != RELOPT_BASEREL)
1137 continue;
1138
1140 }
1141}
static void extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
Definition initsplan.c:1144

References Assert, extract_lateral_references(), fb(), RELOPT_BASEREL, and root.

Referenced by query_planner().

◆ find_minmax_agg_replacement_param()

Param * find_minmax_agg_replacement_param ( PlannerInfo root,
Aggref aggref 
)
extern

Definition at line 3599 of file setrefs.c.

3600{
3601 if (root->minmax_aggs != NIL &&
3602 list_length(aggref->args) == 1)
3603 {
3605 ListCell *lc;
3606
3607 foreach(lc, root->minmax_aggs)
3608 {
3610
3611 if (mminfo->aggfnoid == aggref->aggfnoid &&
3612 equal(mminfo->target, curTarget->expr))
3613 return mminfo->param;
3614 }
3615 }
3616 return NULL;
3617}
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:464
List * args
Definition primnodes.h:488
Param * param
Definition pathnodes.h:3479

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

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

Definition at line 1357 of file analyzejoins.c.

1364{
1365 return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
1366 jointype, restrictlist, force_cache, NULL);
1367}
bool innerrel_is_unique_ext(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache, List **extra_clauses)

References fb(), 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 
)
extern

Definition at line 1379 of file analyzejoins.c.

1387{
1389 ListCell *lc;
1391 List *outer_exprs = NIL;
1392 bool self_join = (extra_clauses != NULL);
1393
1394 /* Certainly can't prove uniqueness when there are no joinclauses */
1395 if (restrictlist == NIL)
1396 return false;
1397
1398 /*
1399 * Make a quick check to eliminate cases in which we will surely be unable
1400 * to prove uniqueness of the innerrel.
1401 */
1402 if (!rel_supports_distinctness(root, innerrel))
1403 return false;
1404
1405 /*
1406 * Query the cache to see if we've managed to prove that innerrel is
1407 * unique for any subset of this outerrel. For non-self-join search, we
1408 * don't need an exact match, as extra outerrels can't make the innerrel
1409 * any less unique (or more formally, the restrictlist for a join to a
1410 * superset outerrel must be a superset of the conditions we successfully
1411 * used before). For self-join search, we require an exact match of
1412 * outerrels because we need extra clauses to be valid for our case. Also,
1413 * for self-join checking we've filtered the clauses list. Thus, we can
1414 * match only the result cached for a self-join search for another
1415 * self-join check.
1416 */
1417 foreach(lc, innerrel->unique_for_rels)
1418 {
1420
1421 if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
1422 (self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
1423 uniqueRelInfo->self_join))
1424 {
1425 if (extra_clauses)
1426 *extra_clauses = uniqueRelInfo->extra_clauses;
1427 return true; /* Success! */
1428 }
1429 }
1430
1431 /*
1432 * Conversely, we may have already determined that this outerrel, or some
1433 * superset thereof, cannot prove this innerrel to be unique.
1434 */
1435 foreach(lc, innerrel->non_unique_for_rels)
1436 {
1437 Relids unique_for_rels = (Relids) lfirst(lc);
1438
1439 if (bms_is_subset(outerrelids, unique_for_rels))
1440 return false;
1441 }
1442
1443 /* No cached information, so try to make the proof. */
1444 if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1445 jointype, restrictlist,
1446 self_join ? &outer_exprs : NULL))
1447 {
1448 /*
1449 * Cache the positive result for future probes, being sure to keep it
1450 * in the planner_cxt even if we are working in GEQO.
1451 *
1452 * Note: one might consider trying to isolate the minimal subset of
1453 * the outerrels that proved the innerrel unique. But it's not worth
1454 * the trouble, because the planner builds up joinrels incrementally
1455 * and so we'll see the minimally sufficient outerrels before any
1456 * supersets of them anyway.
1457 */
1458 old_context = MemoryContextSwitchTo(root->planner_cxt);
1460 uniqueRelInfo->outerrelids = bms_copy(outerrelids);
1461 uniqueRelInfo->self_join = self_join;
1462 uniqueRelInfo->extra_clauses = outer_exprs;
1463 innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1466
1467 if (extra_clauses)
1468 *extra_clauses = outer_exprs;
1469 return true; /* Success! */
1470 }
1471 else
1472 {
1473 /*
1474 * None of the join conditions for outerrel proved innerrel unique, so
1475 * we can safely reject this outerrel or any subset of it in future
1476 * checks.
1477 *
1478 * However, in normal planning mode, caching this knowledge is totally
1479 * pointless; it won't be queried again, because we build up joinrels
1480 * from smaller to larger. It's only useful when using GEQO or
1481 * another planner extension that attempts planning multiple times.
1482 *
1483 * Also, allow callers to override that heuristic and force caching;
1484 * that's useful for reduce_unique_semijoins, which calls here before
1485 * the normal join search starts.
1486 */
1487 if (force_cache || root->assumeReplanning)
1488 {
1489 old_context = MemoryContextSwitchTo(root->planner_cxt);
1490 innerrel->non_unique_for_rels =
1491 lappend(innerrel->non_unique_for_rels,
1492 bms_copy(outerrelids));
1494 }
1495
1496 return false;
1497 }
1498}
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)
#define makeNode(_type_)
Definition nodes.h:161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
Bitmapset * Relids
Definition pathnodes.h:103
List * unique_for_rels
Definition pathnodes.h:1124
List * non_unique_for_rels
Definition pathnodes.h:1126

References bms_copy(), bms_equal(), bms_is_subset(), fb(), is_innerrel_unique_for(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, rel_supports_distinctness(), root, 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)
extern

Definition at line 7231 of file createplan.c.

7232{
7233 /* Most plan types can project, so just list the ones that can't */
7234 switch (path->pathtype)
7235 {
7236 case T_Hash:
7237 case T_Material:
7238 case T_Memoize:
7239 case T_Sort:
7240 case T_IncrementalSort:
7241 case T_Unique:
7242 case T_SetOp:
7243 case T_LockRows:
7244 case T_Limit:
7245 case T_ModifyTable:
7246 case T_MergeAppend:
7247 case T_RecursiveUnion:
7248 return false;
7249 case T_CustomScan:
7251 return true;
7252 return false;
7253 case T_Append:
7254
7255 /*
7256 * Append can't project, but if an AppendPath is being used to
7257 * represent a dummy path, what will actually be generated is a
7258 * Result which can project.
7259 */
7260 return IS_DUMMY_APPEND(path);
7261 case T_ProjectSet:
7262
7263 /*
7264 * Although ProjectSet certainly projects, say "no" because we
7265 * don't want the planner to randomly replace its tlist with
7266 * something else; the SRFs have to stay at top level. This might
7267 * get relaxed later.
7268 */
7269 return false;
7270 default:
7271 break;
7272 }
7273 return true;
7274}
#define CUSTOMPATH_SUPPORT_PROJECTION
Definition extensible.h:86
#define castNode(_type_, nodeptr)
Definition nodes.h:182
#define IS_DUMMY_APPEND(p)
Definition pathnodes.h:2291
NodeTag pathtype
Definition pathnodes.h:1971

References castNode, CUSTOMPATH_SUPPORT_PROJECTION, fb(), 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)
extern

Definition at line 7281 of file createplan.c.

7282{
7283 /* Most plan types can project, so just list the ones that can't */
7284 switch (nodeTag(plan))
7285 {
7286 case T_Hash:
7287 case T_Material:
7288 case T_Memoize:
7289 case T_Sort:
7290 case T_Unique:
7291 case T_SetOp:
7292 case T_LockRows:
7293 case T_Limit:
7294 case T_ModifyTable:
7295 case T_Append:
7296 case T_MergeAppend:
7297 case T_RecursiveUnion:
7298 return false;
7299 case T_CustomScan:
7301 return true;
7302 return false;
7303 case T_ProjectSet:
7304
7305 /*
7306 * Although ProjectSet certainly projects, say "no" because we
7307 * don't want the planner to randomly replace its tlist with
7308 * something else; the SRFs have to stay at top level. This might
7309 * get relaxed later.
7310 */
7311 return false;
7312 default:
7313 break;
7314 }
7315 return true;
7316}

References CUSTOMPATH_SUPPORT_PROJECTION, fb(), 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,
Cardinality  numGroups,
Size  transitionSpace,
Plan lefttree 
)
extern

Definition at line 6583 of file createplan.c.

6588{
6589 Agg *node = makeNode(Agg);
6590 Plan *plan = &node->plan;
6591
6592 node->aggstrategy = aggstrategy;
6593 node->aggsplit = aggsplit;
6594 node->numCols = numGroupCols;
6595 node->grpColIdx = grpColIdx;
6596 node->grpOperators = grpOperators;
6597 node->grpCollations = grpCollations;
6598 node->numGroups = numGroups;
6599 node->transitionSpace = transitionSpace;
6600 node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6601 node->groupingSets = groupingSets;
6602 node->chain = chain;
6603
6604 plan->qual = qual;
6605 plan->targetlist = tlist;
6606 plan->lefttree = lefttree;
6607 plan->righttree = NULL;
6608
6609 return node;
6610}
AggSplit aggsplit
Definition plannodes.h:1217
List * chain
Definition plannodes.h:1244
List * groupingSets
Definition plannodes.h:1241
Bitmapset * aggParams
Definition plannodes.h:1236
Cardinality numGroups
Definition plannodes.h:1230
Plan plan
Definition plannodes.h:1211
int numCols
Definition plannodes.h:1220
uint64 transitionSpace
Definition plannodes.h:1233
AggStrategy aggstrategy
Definition plannodes.h:1214
struct Plan * righttree
Definition plannodes.h:240

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

Referenced by create_agg_plan(), and create_groupingsets_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 
)
extern

Definition at line 5803 of file createplan.c.

5811{
5813 Plan *plan = &node->scan.plan;
5814
5815 /* cost will be filled in by create_foreignscan_plan */
5816 plan->targetlist = qptlist;
5817 plan->qual = qpqual;
5818 plan->lefttree = outer_plan;
5819 plan->righttree = NULL;
5820 node->scan.scanrelid = scanrelid;
5821
5822 /* these may be overridden by the FDW's PlanDirectModify callback. */
5823 node->operation = CMD_SELECT;
5824 node->resultRelation = 0;
5825
5826 /* checkAsUser, fs_server will be filled in by create_foreignscan_plan */
5827 node->checkAsUser = InvalidOid;
5828 node->fs_server = InvalidOid;
5829 node->fdw_exprs = fdw_exprs;
5830 node->fdw_private = fdw_private;
5831 node->fdw_scan_tlist = fdw_scan_tlist;
5832 node->fdw_recheck_quals = fdw_recheck_quals;
5833 /* fs_relids, fs_base_relids will be filled by create_foreignscan_plan */
5834 node->fs_relids = NULL;
5835 node->fs_base_relids = NULL;
5836 /* fsSystemCol will be filled in by create_foreignscan_plan */
5837 node->fsSystemCol = false;
5838
5839 return node;
5840}
@ CMD_SELECT
Definition nodes.h:275
Oid checkAsUser
Definition plannodes.h:898
CmdType operation
Definition plannodes.h:894
List * fdw_exprs
Definition plannodes.h:902
bool fsSystemCol
Definition plannodes.h:914
Bitmapset * fs_relids
Definition plannodes.h:910
List * fdw_private
Definition plannodes.h:904
Bitmapset * fs_base_relids
Definition plannodes.h:912
Index resultRelation
Definition plannodes.h:896
List * fdw_recheck_quals
Definition plannodes.h:908
List * fdw_scan_tlist
Definition plannodes.h:906
Index scanrelid
Definition plannodes.h:544

References ForeignScan::checkAsUser, CMD_SELECT, fb(), 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, Plan::righttree, 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 
)
extern

Definition at line 6902 of file createplan.c.

6905{
6906 Limit *node = makeNode(Limit);
6907 Plan *plan = &node->plan;
6908
6909 plan->targetlist = lefttree->targetlist;
6910 plan->qual = NIL;
6911 plan->lefttree = lefttree;
6912 plan->righttree = NULL;
6913
6914 node->limitOffset = limitOffset;
6915 node->limitCount = limitCount;
6916 node->limitOption = limitOption;
6917 node->uniqNumCols = uniqNumCols;
6918 node->uniqColIdx = uniqColIdx;
6919 node->uniqOperators = uniqOperators;
6920 node->uniqCollations = uniqCollations;
6921
6922 return node;
6923}
LimitOption limitOption
Definition plannodes.h:1509
Plan plan
Definition plannodes.h:1500
Node * limitCount
Definition plannodes.h:1506
int uniqNumCols
Definition plannodes.h:1512
Node * limitOffset
Definition plannodes.h:1503

References fb(), Limit::limitCount, Limit::limitOffset, Limit::limitOption, makeNode, NIL, Limit::plan, plan, Plan::righttree, 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 
)
extern

Definition at line 6397 of file createplan.c.

6398{
6399 List *sub_tlist = lefttree->targetlist;
6400 ListCell *l;
6401 int numsortkeys;
6402 AttrNumber *sortColIdx;
6403 Oid *sortOperators;
6404 Oid *collations;
6405 bool *nullsFirst;
6406
6407 /* Convert list-ish representation to arrays wanted by executor */
6409 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6410 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6411 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6412 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6413
6414 numsortkeys = 0;
6415 foreach(l, sortcls)
6416 {
6419
6420 sortColIdx[numsortkeys] = tle->resno;
6421 sortOperators[numsortkeys] = sortcl->sortop;
6422 collations[numsortkeys] = exprCollation((Node *) tle->expr);
6423 nullsFirst[numsortkeys] = sortcl->nulls_first;
6424 numsortkeys++;
6425 }
6426
6427 return make_sort(lefttree, numsortkeys,
6428 sortColIdx, sortOperators,
6429 collations, nullsFirst);
6430}
int16 AttrNumber
Definition attnum.h:21
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
void * palloc(Size size)
Definition mcxt.c:1390
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
unsigned int Oid
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition tlist.c:376

References exprCollation(), fb(), get_sortgroupclause_tle(), lfirst, list_length(), make_sort(), palloc(), and Plan::targetlist.

◆ match_foreign_keys_to_quals()

void match_foreign_keys_to_quals ( PlannerInfo root)
extern

Definition at line 4033 of file initsplan.c.

4034{
4035 List *newlist = NIL;
4036 ListCell *lc;
4037
4038 foreach(lc, root->fkey_list)
4039 {
4043 int colno;
4044
4045 /*
4046 * Either relid might identify a rel that is in the query's rtable but
4047 * isn't referenced by the jointree, or has been removed by join
4048 * removal, so that it won't have a RelOptInfo. Hence don't use
4049 * find_base_rel() here. We can ignore such FKs.
4050 */
4051 if (fkinfo->con_relid >= root->simple_rel_array_size ||
4052 fkinfo->ref_relid >= root->simple_rel_array_size)
4053 continue; /* just paranoia */
4054 con_rel = root->simple_rel_array[fkinfo->con_relid];
4055 if (con_rel == NULL)
4056 continue;
4057 ref_rel = root->simple_rel_array[fkinfo->ref_relid];
4058 if (ref_rel == NULL)
4059 continue;
4060
4061 /*
4062 * Ignore FK unless both rels are baserels. This gets rid of FKs that
4063 * link to inheritance child rels (otherrels).
4064 */
4065 if (con_rel->reloptkind != RELOPT_BASEREL ||
4066 ref_rel->reloptkind != RELOPT_BASEREL)
4067 continue;
4068
4069 /*
4070 * Scan the columns and try to match them to eclasses and quals.
4071 *
4072 * Note: for simple inner joins, any match should be in an eclass.
4073 * "Loose" quals that syntactically match an FK equality must have
4074 * been rejected for EC status because they are outer-join quals or
4075 * similar. We can still consider them to match the FK.
4076 */
4077 for (colno = 0; colno < fkinfo->nkeys; colno++)
4078 {
4079 EquivalenceClass *ec;
4081 ref_attno;
4082 Oid fpeqop;
4083 ListCell *lc2;
4084
4086 /* Don't bother looking for loose quals if we got an EC match */
4087 if (ec != NULL)
4088 {
4089 fkinfo->nmatched_ec++;
4090 if (ec->ec_has_const)
4091 fkinfo->nconst_ec++;
4092 continue;
4093 }
4094
4095 /*
4096 * Scan joininfo list for relevant clauses. Either rel's joininfo
4097 * list would do equally well; we use con_rel's.
4098 */
4099 con_attno = fkinfo->conkey[colno];
4100 ref_attno = fkinfo->confkey[colno];
4101 fpeqop = InvalidOid; /* we'll look this up only if needed */
4102
4103 foreach(lc2, con_rel->joininfo)
4104 {
4105 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
4106 OpExpr *clause = (OpExpr *) rinfo->clause;
4107 Var *leftvar;
4108 Var *rightvar;
4109
4110 /* Only binary OpExprs are useful for consideration */
4111 if (!IsA(clause, OpExpr) ||
4112 list_length(clause->args) != 2)
4113 continue;
4114 leftvar = (Var *) get_leftop((Expr *) clause);
4115 rightvar = (Var *) get_rightop((Expr *) clause);
4116
4117 /* Operands must be Vars, possibly with RelabelType */
4118 while (leftvar && IsA(leftvar, RelabelType))
4119 leftvar = (Var *) ((RelabelType *) leftvar)->arg;
4120 if (!(leftvar && IsA(leftvar, Var)))
4121 continue;
4122 while (rightvar && IsA(rightvar, RelabelType))
4123 rightvar = (Var *) ((RelabelType *) rightvar)->arg;
4124 if (!(rightvar && IsA(rightvar, Var)))
4125 continue;
4126
4127 /* Now try to match the vars to the current foreign key cols */
4128 if (fkinfo->ref_relid == leftvar->varno &&
4129 ref_attno == leftvar->varattno &&
4130 fkinfo->con_relid == rightvar->varno &&
4131 con_attno == rightvar->varattno)
4132 {
4133 /* Vars match, but is it the right operator? */
4134 if (clause->opno == fkinfo->conpfeqop[colno])
4135 {
4136 fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
4137 rinfo);
4138 fkinfo->nmatched_ri++;
4139 }
4140 }
4141 else if (fkinfo->ref_relid == rightvar->varno &&
4142 ref_attno == rightvar->varattno &&
4143 fkinfo->con_relid == leftvar->varno &&
4144 con_attno == leftvar->varattno)
4145 {
4146 /*
4147 * Reverse match, must check commutator operator. Look it
4148 * up if we didn't already. (In the worst case we might
4149 * do multiple lookups here, but that would require an FK
4150 * equality operator without commutator, which is
4151 * unlikely.)
4152 */
4153 if (!OidIsValid(fpeqop))
4154 fpeqop = get_commutator(fkinfo->conpfeqop[colno]);
4155 if (clause->opno == fpeqop)
4156 {
4157 fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
4158 rinfo);
4159 fkinfo->nmatched_ri++;
4160 }
4161 }
4162 }
4163 /* If we found any matching loose quals, count col as matched */
4164 if (fkinfo->rinfos[colno])
4165 fkinfo->nmatched_rcols++;
4166 }
4167
4168 /*
4169 * Currently, we drop multicolumn FKs that aren't fully matched to the
4170 * query. Later we might figure out how to derive some sort of
4171 * estimate from them, in which case this test should be weakened to
4172 * "if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) > 0)".
4173 */
4174 if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) == fkinfo->nkeys)
4176 }
4177 /* Replace fkey_list, thereby discarding any useless entries */
4178 root->fkey_list = newlist;
4179}
EquivalenceClass * match_eclasses_to_foreign_key_col(PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
Oid get_commutator(Oid opno)
Definition lsyscache.c:1740
static Node * get_rightop(const void *clause)
Definition nodeFuncs.h:95
static Node * get_leftop(const void *clause)
Definition nodeFuncs.h:83
Expr * clause
Definition pathnodes.h:2901

References OpExpr::args, RestrictInfo::clause, EquivalenceClass::ec_has_const, fb(), get_commutator(), get_leftop(), get_rightop(), InvalidOid, IsA, lappend(), lfirst, list_length(), match_eclasses_to_foreign_key_col(), NIL, OidIsValid, OpExpr::opno, RELOPT_BASEREL, and root.

Referenced by query_planner().

◆ materialize_finished_plan()

Plan * materialize_finished_plan ( Plan subplan)
extern

Definition at line 6509 of file createplan.c.

6510{
6511 Plan *matplan;
6512 Path matpath; /* dummy for cost_material */
6514 bool unsafe_initplans;
6515
6516 matplan = (Plan *) make_material(subplan);
6517
6518 /*
6519 * XXX horrid kluge: if there are any initPlans attached to the subplan,
6520 * move them up to the Material node, which is now effectively the top
6521 * plan node in its query level. This prevents failure in
6522 * SS_finalize_plan(), which see for comments.
6523 */
6524 matplan->initPlan = subplan->initPlan;
6525 subplan->initPlan = NIL;
6526
6527 /* Move the initplans' cost delta, as well */
6530 subplan->startup_cost -= initplan_cost;
6531 subplan->total_cost -= initplan_cost;
6532
6533 /* Set cost data */
6536 subplan->disabled_nodes,
6537 subplan->startup_cost,
6538 subplan->total_cost,
6539 subplan->plan_rows,
6540 subplan->plan_width);
6541 matplan->disabled_nodes = subplan->disabled_nodes;
6542 matplan->startup_cost = matpath.startup_cost + initplan_cost;
6543 matplan->total_cost = matpath.total_cost + initplan_cost;
6544 matplan->plan_rows = subplan->plan_rows;
6545 matplan->plan_width = subplan->plan_width;
6546 matplan->parallel_aware = false;
6547 matplan->parallel_safe = subplan->parallel_safe;
6548
6549 return matplan;
6550}
void cost_material(Path *path, bool enabled, int input_disabled_nodes, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition costsize.c:2583
bool enable_material
Definition costsize.c:155
static Material * make_material(Plan *lefttree)
double Cost
Definition nodes.h:261
Cost total_cost
Definition plannodes.h:205
Cost startup_cost
Definition plannodes.h:203
int plan_width
Definition plannodes.h:213
Cardinality plan_rows
Definition plannodes.h:211
int disabled_nodes
Definition plannodes.h:201
List * initPlan
Definition plannodes.h:242
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition subselect.c:2493

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

Referenced by build_subplan(), and standard_planner().

◆ preprocess_minmax_aggregates()

void preprocess_minmax_aggregates ( PlannerInfo root)
extern

Definition at line 74 of file planagg.c.

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

References add_path(), Assert, build_minmax_path(), can_minmax_aggs(), create_minmaxagg_path(), create_pathtarget, elog, ERROR, exprCollation(), exprType(), fb(), fetch_upper_rel(), FromExpr::fromlist, get_equality_op_for_ordering_op(), IsA, lfirst, linitial, list_length(), NIL, OidIsValid, MinMaxAggInfo::param, parse(), planner_rt_fetch, root, RTE_RELATION, RTE_SUBQUERY, SS_make_initplan_output_param(), 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 
)
extern

Definition at line 3714 of file initsplan.c.

3722{
3724 Node *clause;
3725 Relids relids;
3726 bool pseudoconstant = false;
3727
3728 /*
3729 * Build the new clause. Copy to ensure it shares no substructure with
3730 * original (this is necessary in case there are subselects in there...)
3731 */
3732 clause = (Node *) make_opclause(opno,
3733 BOOLOID, /* opresulttype */
3734 false, /* opretset */
3737 InvalidOid,
3738 collation);
3739
3740 /* If both constant, try to reduce to a boolean constant. */
3741 if (both_const)
3742 {
3743 clause = eval_const_expressions(root, clause);
3744
3745 /* If we produced const TRUE, just drop the clause */
3746 if (clause && IsA(clause, Const))
3747 {
3748 Const *cclause = (Const *) clause;
3749
3750 Assert(cclause->consttype == BOOLOID);
3751 if (!cclause->constisnull && DatumGetBool(cclause->constvalue))
3752 return NULL;
3753 }
3754 }
3755
3756 /*
3757 * The rest of this is a very cut-down version of distribute_qual_to_rels.
3758 * We can skip most of the work therein, but there are a couple of special
3759 * cases we still have to handle.
3760 *
3761 * Retrieve all relids mentioned within the possibly-simplified clause.
3762 */
3763 relids = pull_varnos(root, clause);
3764 Assert(bms_is_subset(relids, qualscope));
3765
3766 /*
3767 * If the clause is variable-free, our normal heuristic for pushing it
3768 * down to just the mentioned rels doesn't work, because there are none.
3769 * Apply it as a gating qual at the appropriate level (see comments for
3770 * get_join_domain_min_rels).
3771 */
3772 if (bms_is_empty(relids))
3773 {
3774 /* eval at join domain's safe level */
3775 relids = get_join_domain_min_rels(root, qualscope);
3776 /* mark as gating qual */
3777 pseudoconstant = true;
3778 /* tell createplan.c to check for gating quals */
3779 root->hasPseudoConstantQuals = true;
3780 }
3781
3782 /*
3783 * Build the RestrictInfo node itself.
3784 */
3786 (Expr *) clause,
3787 true, /* is_pushed_down */
3788 false, /* !has_clone */
3789 false, /* !is_clone */
3791 security_level,
3792 relids,
3793 NULL, /* incompatible_relids */
3794 NULL); /* outer_relids */
3795
3796 /*
3797 * If it's a join clause, add vars used in the clause to targetlists of
3798 * their relations, so that they will be emitted by the plan nodes that
3799 * scan those relations (else they won't be available at the join node!).
3800 *
3801 * Typically, we'd have already done this when the component expressions
3802 * were first seen by distribute_qual_to_rels; but it is possible that
3803 * some of the Vars could have missed having that done because they only
3804 * appeared in single-relation clauses originally. So do it here for
3805 * safety.
3806 *
3807 * See also rebuild_joinclause_attr_needed, which has to partially repeat
3808 * this work after removal of an outer join. (Since we will put this
3809 * clause into the joininfo lists, that function needn't do any extra work
3810 * to find it.)
3811 */
3812 if (bms_membership(relids) == BMS_MULTIPLE)
3813 {
3814 List *vars = pull_var_clause(clause,
3818
3820 list_free(vars);
3821 }
3822
3823 /*
3824 * Check mergejoinability. This will usually succeed, since the op came
3825 * from an EquivalenceClass; but we could have reduced the original clause
3826 * to a constant.
3827 */
3829
3830 /*
3831 * Note we don't do initialize_mergeclause_eclasses(); the caller can
3832 * handle that much more cheaply than we can. It's okay to call
3833 * distribute_restrictinfo_to_rels() before that happens.
3834 */
3835
3836 /*
3837 * Push the new clause into all the appropriate restrictinfo lists.
3838 */
3840
3841 return restrictinfo;
3842}
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
@ BMS_MULTIPLE
Definition bitmapset.h:73
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition clauses.c:2500
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3629
static Relids get_join_domain_min_rels(PlannerInfo *root, Relids domain_relids)
Definition initsplan.c:3927
static bool DatumGetBool(Datum X)
Definition postgres.h:100
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(), copyObject, DatumGetBool(), distribute_restrictinfo_to_rels(), eval_const_expressions(), fb(), 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, 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 distinct_cols 
)
extern

Definition at line 1161 of file analyzejoins.c.

1162{
1163 ListCell *l;
1165
1166 /*
1167 * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
1168 * columns in the DISTINCT clause appear in colnos and operator semantics
1169 * match. This is true even if there are SRFs in the DISTINCT columns or
1170 * elsewhere in the tlist.
1171 */
1172 if (query->distinctClause)
1173 {
1174 foreach(l, query->distinctClause)
1175 {
1178 query->targetList);
1179
1181 if (dcinfo == NULL ||
1182 !equality_ops_are_compatible(dcinfo->opid, sgc->eqop) ||
1184 exprCollation((Node *) tle->expr)))
1185 break; /* exit early if no match */
1186 }
1187 if (l == NULL) /* had matches for all? */
1188 return true;
1189 }
1190
1191 /*
1192 * Otherwise, a set-returning function in the query's targetlist can
1193 * result in returning duplicate rows, despite any grouping that might
1194 * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
1195 * columns, it would be safe because they'd be expanded before grouping.
1196 * But it doesn't currently seem worth the effort to check for that.)
1197 */
1198 if (query->hasTargetSRFs)
1199 return false;
1200
1201 /*
1202 * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
1203 * the grouped columns appear in colnos and operator semantics match.
1204 */
1205 if (query->groupClause && !query->groupingSets)
1206 {
1207 foreach(l, query->groupClause)
1208 {
1211 query->targetList);
1212
1214 if (dcinfo == NULL ||
1215 !equality_ops_are_compatible(dcinfo->opid, sgc->eqop) ||
1217 exprCollation((Node *) tle->expr)))
1218 break; /* exit early if no match */
1219 }
1220 if (l == NULL) /* had matches for all? */
1221 return true;
1222 }
1223 else if (query->groupingSets)
1224 {
1225 List *gsets;
1226
1227 /*
1228 * If we have grouping sets with expressions, we probably don't have
1229 * uniqueness and analysis would be hard. Punt.
1230 */
1231 if (query->groupClause)
1232 return false;
1233
1234 /*
1235 * If we have no groupClause (therefore no grouping expressions), we
1236 * might have one or many empty grouping sets. If there's just one,
1237 * or if the DISTINCT clause is used on the GROUP BY, then we're
1238 * returning only one row and are certainly unique. But otherwise, we
1239 * know we're certainly not unique.
1240 */
1241 if (query->groupDistinct)
1242 return true;
1243
1244 gsets = expand_grouping_sets(query->groupingSets, false, -1);
1245
1246 return (list_length(gsets) == 1);
1247 }
1248 else
1249 {
1250 /*
1251 * If we have no GROUP BY, but do have aggregates or HAVING, then the
1252 * result is at most one row so it's surely unique, for any operators.
1253 */
1254 if (query->hasAggs || query->havingQual)
1255 return true;
1256 }
1257
1258 /*
1259 * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
1260 * except with ALL.
1261 */
1262 if (query->setOperations)
1263 {
1265
1266 Assert(topop->op != SETOP_NONE);
1267
1268 if (!topop->all)
1269 {
1270 ListCell *lg;
1271
1272 /* We're good if all the nonjunk output columns are in colnos */
1273 lg = list_head(topop->groupClauses);
1274 foreach(l, query->targetList)
1275 {
1278
1279 if (tle->resjunk)
1280 continue; /* ignore resjunk columns */
1281
1282 /* non-resjunk columns should have grouping clauses */
1283 Assert(lg != NULL);
1285 lg = lnext(topop->groupClauses, lg);
1286
1288 if (dcinfo == NULL ||
1289 !equality_ops_are_compatible(dcinfo->opid, sgc->eqop) ||
1291 exprCollation((Node *) tle->expr)))
1292 break; /* exit early if no match */
1293 }
1294 if (l == NULL) /* had matches for all? */
1295 return true;
1296 }
1297 }
1298
1299 /*
1300 * XXX Are there any other cases in which we can easily see the result
1301 * must be distinct?
1302 *
1303 * If you do add more smarts to this function, be sure to update
1304 * query_supports_distinctness() to match.
1305 */
1306
1307 return false;
1308}
static DistinctColInfo * distinct_col_search(int colno, List *distinct_cols)
bool collations_agree_on_equality(Oid coll1, Oid coll2)
Definition lsyscache.c:886
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition lsyscache.c:773
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Definition parse_agg.c:2019
@ SETOP_NONE
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:375
bool groupDistinct
Definition parsenodes.h:220
Node * setOperations
Definition parsenodes.h:239
List * groupClause
Definition parsenodes.h:219
Node * havingQual
Definition parsenodes.h:225
List * targetList
Definition parsenodes.h:201
List * groupingSets
Definition parsenodes.h:223
List * distinctClause
Definition parsenodes.h:229

References Assert, castNode, collations_agree_on_equality(), distinct_col_search(), Query::distinctClause, equality_ops_are_compatible(), expand_grouping_sets(), exprCollation(), fb(), get_sortgroupclause_tle(), Query::groupClause, Query::groupDistinct, Query::groupingSets, Query::havingQual, lfirst, list_head(), list_length(), lnext(), SETOP_NONE, Query::setOperations, and Query::targetList.

Referenced by rel_is_distinct_for().

◆ query_planner()

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

Definition at line 54 of file planmain.c.

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

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(), 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(), fb(), 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(), remove_useless_groupby_columns(), remove_useless_joins(), remove_useless_self_joins(), root, RTE_RESULT, set_cheapest(), setup_eager_aggregation(), and setup_simple_rel_arrays().

Referenced by build_minmax_path(), and grouping_planner().

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)
extern

Definition at line 1114 of file analyzejoins.c.

1115{
1116 /* SRFs break distinctness except with DISTINCT, see below */
1117 if (query->hasTargetSRFs && query->distinctClause == NIL)
1118 return false;
1119
1120 /* check for features we can prove distinctness with */
1121 if (query->distinctClause != NIL ||
1122 query->groupClause != NIL ||
1123 query->groupingSets != NIL ||
1124 query->hasAggs ||
1125 query->havingQual ||
1126 query->setOperations)
1127 return true;
1128
1129 return false;
1130}

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

Referenced by rel_supports_distinctness().

◆ rebuild_joinclause_attr_needed()

void rebuild_joinclause_attr_needed ( PlannerInfo root)
extern

Definition at line 3961 of file initsplan.c.

3962{
3963 /*
3964 * We must examine all join clauses, but there's no value in processing
3965 * any join clause more than once. So it's slightly annoying that we have
3966 * to find them via the per-base-relation joininfo lists. Avoid duplicate
3967 * processing by tracking the rinfo_serial numbers of join clauses we've
3968 * already seen. (This doesn't work for is_clone clauses, so we must
3969 * waste effort on them.)
3970 */
3972 Index rti;
3973
3974 /* Scan all baserels for join clauses */
3975 for (rti = 1; rti < root->simple_rel_array_size; rti++)
3976 {
3977 RelOptInfo *brel = root->simple_rel_array[rti];
3978 ListCell *lc;
3979
3980 if (brel == NULL)
3981 continue;
3982 if (brel->reloptkind != RELOPT_BASEREL)
3983 continue;
3984
3985 foreach(lc, brel->joininfo)
3986 {
3987 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3988 Relids relids = rinfo->required_relids;
3989
3990 if (!rinfo->is_clone) /* else serial number is not unique */
3991 {
3993 continue; /* saw it already */
3995 rinfo->rinfo_serial);
3996 }
3997
3998 if (bms_membership(relids) == BMS_MULTIPLE)
3999 {
4000 List *vars = pull_var_clause((Node *) rinfo->clause,
4005
4006 if (rinfo->is_clone)
4007 where_needed = bms_intersect(relids, root->all_baserels);
4008 else
4009 where_needed = relids;
4011 list_free(vars);
4012 }
4013 }
4014 }
4015}
void add_vars_to_attr_needed(PlannerInfo *root, List *vars, Relids where_needed)
Definition initsplan.c:373
Relids required_relids
Definition pathnodes.h:2932

References add_vars_to_attr_needed(), bms_add_member(), bms_intersect(), bms_is_member(), bms_membership(), BMS_MULTIPLE, RestrictInfo::clause, fb(), RestrictInfo::is_clone, lfirst, list_free(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, RELOPT_BASEREL, 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)
extern

Definition at line 1245 of file initsplan.c.

1246{
1247 Index rti;
1248
1249 /* We need do nothing if the query contains no LATERAL RTEs */
1250 if (!root->hasLateralRTEs)
1251 return;
1252
1253 /* Examine the same baserels that find_lateral_references did */
1254 for (rti = 1; rti < root->simple_rel_array_size; rti++)
1255 {
1256 RelOptInfo *brel = root->simple_rel_array[rti];
1258
1259 if (brel == NULL)
1260 continue;
1261 if (brel->reloptkind != RELOPT_BASEREL)
1262 continue;
1263
1264 /*
1265 * We don't need to repeat all of extract_lateral_references, since it
1266 * kindly saved the extracted Vars/PHVs in lateral_vars.
1267 */
1268 if (brel->lateral_vars == NIL)
1269 continue;
1270
1272
1274 }
1275}

References add_vars_to_attr_needed(), bms_make_singleton(), fb(), NIL, RELOPT_BASEREL, 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 
)
extern

Definition at line 3632 of file setrefs.c.

3633{
3634 /*
3635 * For performance reasons, we don't bother to track built-in functions;
3636 * we just assume they'll never change (or at least not in ways that'd
3637 * invalidate plans using them). For this purpose we can consider a
3638 * built-in function to be one with OID less than FirstUnpinnedObjectId.
3639 * Note that the OID generator guarantees never to generate such an OID
3640 * after startup, even at OID wraparound.
3641 */
3642 if (funcid >= (Oid) FirstUnpinnedObjectId)
3643 {
3645
3646 /*
3647 * It would work to use any syscache on pg_proc, but the easiest is
3648 * PROCOID since we already have the function's OID at hand. Note
3649 * that plancache.c knows we use PROCOID.
3650 */
3651 inval_item->cacheId = PROCOID;
3653 ObjectIdGetDatum(funcid));
3654
3655 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3656 }
3657}
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define GetSysCacheHashValue1(cacheId, key1)
Definition syscache.h:118
#define FirstUnpinnedObjectId
Definition transam.h:196

References fb(), FirstUnpinnedObjectId, GetSysCacheHashValue1, lappend(), makeNode, ObjectIdGetDatum(), and root.

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

◆ record_plan_type_dependency()

void record_plan_type_dependency ( PlannerInfo root,
Oid  typid 
)
extern

Definition at line 3672 of file setrefs.c.

3673{
3674 /*
3675 * As in record_plan_function_dependency, ignore the possibility that
3676 * someone would change a built-in domain.
3677 */
3678 if (typid >= (Oid) FirstUnpinnedObjectId)
3679 {
3681
3682 /*
3683 * It would work to use any syscache on pg_type, but the easiest is
3684 * TYPEOID since we already have the type's OID at hand. Note that
3685 * plancache.c knows we use TYPEOID.
3686 */
3687 inval_item->cacheId = TYPEOID;
3689 ObjectIdGetDatum(typid));
3690
3691 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3692 }
3693}

References fb(), FirstUnpinnedObjectId, GetSysCacheHashValue1, lappend(), makeNode, ObjectIdGetDatum(), and root.

Referenced by eval_const_expressions_mutator().

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)
extern

Definition at line 875 of file analyzejoins.c.

876{
877 ListCell *lc;
878
879 /*
880 * Scan the join_info_list to find semijoins.
881 */
882 foreach(lc, root->join_info_list)
883 {
885 int innerrelid;
886 RelOptInfo *innerrel;
888 List *restrictlist;
889
890 /*
891 * Must be a semijoin to a single baserel, else we aren't going to be
892 * able to do anything with it.
893 */
894 if (sjinfo->jointype != JOIN_SEMI)
895 continue;
896
898 continue;
899
900 innerrel = find_base_rel(root, innerrelid);
901
902 /*
903 * Before we trouble to run generate_join_implied_equalities, make a
904 * quick check to eliminate cases in which we will surely be unable to
905 * prove uniqueness of the innerrel.
906 */
907 if (!rel_supports_distinctness(root, innerrel))
908 continue;
909
910 /* Compute the relid set for the join we are considering */
912 Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
913
914 /*
915 * Since we're only considering a single-rel RHS, any join clauses it
916 * has must be clauses linking it to the semijoin's min_lefthand. We
917 * can also consider EC-derived join clauses.
918 */
919 restrictlist =
922 sjinfo->min_lefthand,
923 innerrel,
924 NULL),
925 innerrel->joininfo);
926
927 /* Test whether the innerrel is unique for those clauses. */
929 joinrelids, sjinfo->min_lefthand, innerrel,
930 JOIN_SEMI, restrictlist, true))
931 continue;
932
933 /* OK, remove the SpecialJoinInfo from the list. */
934 root->join_info_list = foreach_delete_current(root->join_info_list, lc);
935 }
936}
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)
List * list_concat(List *list1, const List *list2)
Definition list.c:561
@ JOIN_SEMI
Definition nodes.h:317
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
List * joininfo
Definition pathnodes.h:1148
Relids min_righthand
Definition pathnodes.h:3227
JoinType jointype
Definition pathnodes.h:3230
Relids min_lefthand
Definition pathnodes.h:3226

References Assert, bms_get_singleton_member(), bms_union(), fb(), 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)
extern

Definition at line 432 of file initsplan.c.

433{
434 Query *parse = root->parse;
438 bool tryremove = false;
439 ListCell *lc;
440 int relid;
441
442 /* No chance to do anything if there are less than two GROUP BY items */
443 if (list_length(root->processed_groupClause) < 2)
444 return;
445
446 /* Don't fiddle with the GROUP BY clause if the query has grouping sets */
447 if (parse->groupingSets)
448 return;
449
450 /*
451 * Scan the GROUP BY clause to find GROUP BY items that are simple Vars.
452 * Fill groupbyattnos[k] with a bitmapset of the column attnos of RTE k
453 * that are GROUP BY items, and groupbycols[k] with a parallel list of
454 * GroupByColInfo records. We need the latter so that, when checking a
455 * unique index against this rel's GROUP BY items, we can verify that the
456 * index's notion of equality agrees with at least one GROUP BY item per
457 * index column.
458 */
461 foreach(lc, root->processed_groupClause)
462 {
465 Var *var = (Var *) tle->expr;
466 GroupByColInfo *info;
467
468 /*
469 * Ignore non-Vars and Vars from other query levels.
470 *
471 * XXX in principle, stable expressions containing Vars could also be
472 * removed, if all the Vars are functionally dependent on other GROUP
473 * BY items. But it's not clear that such cases occur often enough to
474 * be worth troubling over.
475 */
476 if (!IsA(var, Var) ||
477 var->varlevelsup > 0)
478 continue;
479
480 /* OK, remember we have this Var */
481 relid = var->varno;
482 Assert(relid <= list_length(parse->rtable));
483
484 /*
485 * If this isn't the first column for this relation then we now have
486 * multiple columns. That means there might be some that can be
487 * removed.
488 */
491 var->varattno - FirstLowInvalidHeapAttributeNumber);
492
493 info = palloc(sizeof(GroupByColInfo));
494 info->attno = var->varattno;
496 info->coll = var->varcollid;
497 groupbycols[relid] = lappend(groupbycols[relid], info);
498 }
499
500 /*
501 * No Vars or didn't find multiple Vars for any relation in the GROUP BY?
502 * If so, nothing can be removed, so don't waste more effort trying.
503 */
504 if (!tryremove)
505 return;
506
507 /*
508 * Consider each relation and see if it is possible to remove some of its
509 * Vars from GROUP BY. For simplicity and speed, we do the actual removal
510 * in a separate pass. Here, we just fill surplusvars[k] with a bitmapset
511 * of the column attnos of RTE k that are removable GROUP BY items.
512 */
513 surplusvars = NULL; /* don't allocate array unless required */
514 relid = 0;
515 foreach(lc, parse->rtable)
516 {
518 RelOptInfo *rel;
522
523 relid++;
524
525 /* Only plain relations could have primary-key constraints */
526 if (rte->rtekind != RTE_RELATION)
527 continue;
528
529 /*
530 * We must skip inheritance parent tables as some of the child rels
531 * may cause duplicate rows. This cannot happen with partitioned
532 * tables, however.
533 */
534 if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
535 continue;
536
537 /* Nothing to do unless this rel has multiple Vars in GROUP BY */
538 relattnos = groupbyattnos[relid];
540 continue;
541
542 rel = root->simple_rel_array[relid];
543
544 /*
545 * Now check each index for this relation to see if there are any with
546 * columns which are a proper subset of the grouping columns for this
547 * relation.
548 */
550 {
552 bool index_check_ok;
553
554 /*
555 * Skip any non-unique and deferrable indexes. Predicate indexes
556 * have not been checked yet, so we must skip those too as the
557 * predOK check that's done later might fail.
558 */
559 if (!index->unique || !index->immediate || index->indpred != NIL)
560 continue;
561
562 /* For simplicity, we currently don't support expression indexes */
563 if (index->indexprs != NIL)
564 continue;
565
567 index_check_ok = true;
568 for (int i = 0; i < index->nkeycolumns; i++)
569 {
570 AttrNumber indkey_attno = index->indexkeys[i];
571 Oid indkey_opfamily = index->opfamily[i];
572 Oid indkey_coll = index->indexcollations[i];
573 ListCell *lc2;
574
575 /*
576 * We must insist that the index columns are all defined NOT
577 * NULL otherwise duplicate NULLs could exist. However, we
578 * can relax this check when the index is defined with NULLS
579 * NOT DISTINCT as there can only be 1 NULL row, therefore
580 * functional dependency on the unique columns is maintained,
581 * despite the NULL.
582 */
583 if (!index->nullsnotdistinct &&
585 {
586 index_check_ok = false;
587 break;
588 }
589
590 /*
591 * The index proves uniqueness only under its own opfamily and
592 * collation. Require some GROUP BY item on this column to
593 * use a compatible eqop and collation, the same check
594 * relation_has_unique_index_for() applies to join clauses.
595 */
596 foreach(lc2, groupbycols[relid])
597 {
599
600 if (info->attno != indkey_attno)
601 continue;
604 break;
605 }
606 if (lc2 == NULL)
607 {
608 index_check_ok = false;
609 break;
610 }
611
612 ind_attnos =
616 }
617
618 if (!index_check_ok)
619 continue;
620
621 /*
622 * Skip any indexes where the indexed columns aren't a proper
623 * subset of the GROUP BY.
624 */
626 continue;
627
628 /*
629 * Record the attribute numbers from the index with the fewest
630 * columns. This allows the largest number of columns to be
631 * removed from the GROUP BY clause. In the future, we may wish
632 * to consider using the narrowest set of columns and looking at
633 * pg_statistic.stawidth as it might be better to use an index
634 * with, say two INT4s, rather than, say, one long varlena column.
635 */
636 if (index->nkeycolumns < best_nkeycolumns)
637 {
639 best_nkeycolumns = index->nkeycolumns;
640 }
641 }
642
643 /* Did we find a suitable index? */
645 {
646 /*
647 * To easily remember whether we've found anything to do, we don't
648 * allocate the surplusvars[] array until we find something.
649 */
650 if (surplusvars == NULL)
652
653 /* Remember the attnos of the removable columns */
655 }
656 }
657
658 /*
659 * If we found any surplus Vars, build a new GROUP BY clause without them.
660 * (Note: this may leave some TLEs with unreferenced ressortgroupref
661 * markings, but that's harmless.)
662 */
663 if (surplusvars != NULL)
664 {
666
667 foreach(lc, root->processed_groupClause)
668 {
671 Var *var = (Var *) tle->expr;
672
673 /*
674 * New list must include non-Vars, outer Vars, and anything not
675 * marked as surplus.
676 */
677 if (!IsA(var, Var) ||
678 var->varlevelsup > 0 ||
680 surplusvars[var->varno]))
682 }
683
684 root->processed_groupClause = new_groupby;
685 }
686}
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:673
int32_t int32
Definition c.h:620
#define palloc0_array(type, count)
Definition fe_memutils.h:92
int i
Definition isn.c:77
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
List * get_mergejoin_opfamilies(Oid opno)
Definition lsyscache.c:430
#define lfirst_node(type, lc)
Definition pg_list.h:176
#define foreach_node(type, var, lst)
Definition pg_list.h:528
AttrNumber attno
Definition initsplan.c:92
List * eq_opfamilies
Definition initsplan.c:93
Bitmapset * notnullattnums
Definition pathnodes.h:1083
List * indexlist
Definition pathnodes.h:1091
Definition type.h:97
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

References Assert, GroupByColInfo::attno, bms_add_member(), bms_difference(), bms_is_empty, bms_is_member(), bms_membership(), BMS_MULTIPLE, BMS_SUBSET1, bms_subset_compare(), GroupByColInfo::coll, collations_agree_on_equality(), GroupByColInfo::eq_opfamilies, fb(), FirstLowInvalidHeapAttributeNumber, foreach_node, get_mergejoin_opfamilies(), get_sortgroupclause_tle(), i, RelOptInfo::indexlist, IsA, lappend(), lfirst, lfirst_node, list_length(), list_member_oid(), NIL, RelOptInfo::notnullattnums, palloc(), palloc0_array, parse(), PG_INT32_MAX, root, RTE_RELATION, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by query_planner().

◆ remove_useless_joins()

List * remove_useless_joins ( PlannerInfo root,
List joinlist 
)
extern

Definition at line 93 of file analyzejoins.c.

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

References bms_singleton_member(), elog, ERROR, fb(), 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 
)
extern

Definition at line 2540 of file analyzejoins.c.

2541{
2543 int relid = -1;
2544
2547 return joinlist;
2548
2549 /*
2550 * Merge pairs of relations participated in self-join. Remove unnecessary
2551 * range table entries.
2552 */
2554
2555 if (unlikely(toRemove != NULL))
2556 {
2557 /* At the end, remove orphaned relation links */
2558 while ((relid = bms_next_member(toRemove, relid)) >= 0)
2559 {
2560 int nremoved = 0;
2561
2563 if (nremoved != 1)
2564 elog(ERROR, "failed to find relation %d in joinlist", relid);
2565 }
2566 }
2567
2568 return joinlist;
2569}
static Relids remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove)
bool enable_self_join_elimination
#define unlikely(x)
Definition c.h:438

References bms_next_member(), elog, enable_self_join_elimination, ERROR, fb(), 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 
)
extern

Definition at line 3558 of file initsplan.c.

3560{
3561 /*
3562 * For a clone clause, we don't have a reliable way to determine if the
3563 * input expression of a NullTest is non-nullable: nullingrel bits in
3564 * clone clauses may not reflect reality, so we dare not draw conclusions
3565 * from clones about whether Vars are guaranteed not-null.
3566 */
3567 if (restrictinfo->has_clone || restrictinfo->is_clone)
3568 return false;
3569
3570 /* Check for NullTest qual */
3571 if (IsA(restrictinfo->clause, NullTest))
3572 {
3573 NullTest *nulltest = (NullTest *) restrictinfo->clause;
3574
3575 /* is this NullTest an IS_NULL qual? */
3576 if (nulltest->nulltesttype != IS_NULL)
3577 return false;
3578
3579 /*
3580 * Empty rows can appear NULL in some contexts and NOT NULL in others,
3581 * so avoid this optimization for row expressions.
3582 */
3583 if (nulltest->argisrow)
3584 return false;
3585
3587 }
3588
3589 /* If it's an OR, check its sub-clauses */
3591 {
3592 ListCell *lc;
3593
3594 Assert(is_orclause(restrictinfo->orclause));
3595
3596 /*
3597 * Currently, when processing OR expressions, we only return true when
3598 * all of the OR branches are always false. This could perhaps be
3599 * expanded to remove OR branches that are provably false. This may
3600 * be a useful thing to do as it could result in the OR being left
3601 * with a single arg. That's useful as it would allow the OR
3602 * condition to be replaced with its single argument which may allow
3603 * use of an index for faster filtering on the remaining condition.
3604 */
3605 foreach(lc, ((BoolExpr *) restrictinfo->orclause)->args)
3606 {
3607 Node *orarg = (Node *) lfirst(lc);
3608
3609 if (!IsA(orarg, RestrictInfo) ||
3611 return false;
3612 }
3613 return true;
3614 }
3615
3616 return false;
3617}
bool expr_is_nonnullable(PlannerInfo *root, Expr *expr, NotNullSource source)
Definition clauses.c:4785
bool restriction_is_always_false(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3558
static bool is_orclause(const void *clause)
Definition nodeFuncs.h:116
@ NOTNULL_SOURCE_RELOPT
Definition optimizer.h:136
@ IS_NULL
Definition primnodes.h:1992
bool restriction_is_or_clause(RestrictInfo *restrictinfo)

References Assert, expr_is_nonnullable(), fb(), IS_NULL, is_orclause(), IsA, lfirst, NOTNULL_SOURCE_RELOPT, restriction_is_always_false(), restriction_is_or_clause(), and root.

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

◆ restriction_is_always_true()

bool restriction_is_always_true ( PlannerInfo root,
RestrictInfo restrictinfo 
)
extern

Definition at line 3493 of file initsplan.c.

3495{
3496 /*
3497 * For a clone clause, we don't have a reliable way to determine if the
3498 * input expression of a NullTest is non-nullable: nullingrel bits in
3499 * clone clauses may not reflect reality, so we dare not draw conclusions
3500 * from clones about whether Vars are guaranteed not-null.
3501 */
3502 if (restrictinfo->has_clone || restrictinfo->is_clone)
3503 return false;
3504
3505 /* Check for NullTest qual */
3506 if (IsA(restrictinfo->clause, NullTest))
3507 {
3508 NullTest *nulltest = (NullTest *) restrictinfo->clause;
3509
3510 /* is this NullTest an IS_NOT_NULL qual? */
3511 if (nulltest->nulltesttype != IS_NOT_NULL)
3512 return false;
3513
3514 /*
3515 * Empty rows can appear NULL in some contexts and NOT NULL in others,
3516 * so avoid this optimization for row expressions.
3517 */
3518 if (nulltest->argisrow)
3519 return false;
3520
3522 }
3523
3524 /* If it's an OR, check its sub-clauses */
3526 {
3527 ListCell *lc;
3528
3529 Assert(is_orclause(restrictinfo->orclause));
3530
3531 /*
3532 * if any of the given OR branches is provably always true then the
3533 * entire condition is true.
3534 */
3535 foreach(lc, ((BoolExpr *) restrictinfo->orclause)->args)
3536 {
3537 Node *orarg = (Node *) lfirst(lc);
3538
3539 if (!IsA(orarg, RestrictInfo))
3540 continue;
3541
3543 return true;
3544 }
3545 }
3546
3547 return false;
3548}
bool restriction_is_always_true(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition initsplan.c:3493
@ IS_NOT_NULL
Definition primnodes.h:1992

References Assert, expr_is_nonnullable(), fb(), IS_NOT_NULL, is_orclause(), IsA, lfirst, NOTNULL_SOURCE_RELOPT, restriction_is_always_true(), restriction_is_or_clause(), and root.

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

◆ set_plan_references()

Plan * set_plan_references ( PlannerInfo root,
Plan plan 
)
extern

Definition at line 291 of file setrefs.c.

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

References add_rtes_to_flat_rtable(), PlannerGlobal::appendRelations, Assert, fb(), PlannerGlobal::finalrowmarks, PlannerGlobal::finalrtable, foreach_current_index, lappend(), lfirst, lfirst_node, list_length(), memcpy(), NIL, palloc0(), palloc_object, plan, result, root, PlanRowMark::rti, set_plan_refs(), and PlannerGlobal::subplans.

Referenced by set_subqueryscan_references(), and standard_planner().

◆ setup_eager_aggregation()

void setup_eager_aggregation ( PlannerInfo root)
extern

Definition at line 694 of file initsplan.c.

695{
696 /*
697 * Don't apply eager aggregation if disabled by user.
698 */
700 return;
701
702 /*
703 * Don't apply eager aggregation if there are no available GROUP BY
704 * clauses.
705 */
706 if (!root->processed_groupClause)
707 return;
708
709 /*
710 * For now we don't try to support grouping sets.
711 */
712 if (root->parse->groupingSets)
713 return;
714
715 /*
716 * For now we don't try to support DISTINCT or ORDER BY aggregates.
717 */
718 if (root->numOrderedAggs > 0)
719 return;
720
721 /*
722 * If there are any aggregates that do not support partial mode, or any
723 * partial aggregates that are non-serializable, do not apply eager
724 * aggregation.
725 */
726 if (root->hasNonPartialAggs || root->hasNonSerialAggs)
727 return;
728
729 /*
730 * We don't try to apply eager aggregation if there are set-returning
731 * functions in targetlist.
732 */
733 if (root->parse->hasTargetSRFs)
734 return;
735
736 /*
737 * Eager aggregation only makes sense if there are multiple base rels in
738 * the query.
739 */
740 if (bms_membership(root->all_baserels) != BMS_MULTIPLE)
741 return;
742
743 /*
744 * Don't apply eager aggregation if any aggregate poses a risk of
745 * excessive memory usage during partial aggregation.
746 */
748 return;
749
750 /*
751 * Collect aggregate expressions and plain Vars that appear in the
752 * targetlist and havingQual.
753 */
755
756 /*
757 * If there are no suitable aggregate expressions, we cannot apply eager
758 * aggregation.
759 */
760 if (root->agg_clause_list == NIL)
761 return;
762
763 /*
764 * Collect grouping expressions that appear in grouping clauses.
765 */
767}
bool enable_eager_aggregate
Definition allpaths.c:82
static void create_grouping_expr_infos(PlannerInfo *root)
Definition initsplan.c:931
static bool is_partial_agg_memory_risky(PlannerInfo *root)
Definition initsplan.c:781
static void create_agg_clause_infos(PlannerInfo *root)
Definition initsplan.c:802

References bms_membership(), BMS_MULTIPLE, create_agg_clause_infos(), create_grouping_expr_infos(), enable_eager_aggregate, is_partial_agg_memory_risky(), NIL, and root.

Referenced by query_planner().

◆ trivial_subqueryscan()

bool trivial_subqueryscan ( SubqueryScan plan)
extern

Definition at line 1528 of file setrefs.c.

1529{
1530 int attrno;
1531 ListCell *lp,
1532 *lc;
1533
1534 /* We might have detected this already; in which case reuse the result */
1535 if (plan->scanstatus == SUBQUERY_SCAN_TRIVIAL)
1536 return true;
1537 if (plan->scanstatus == SUBQUERY_SCAN_NONTRIVIAL)
1538 return false;
1539 Assert(plan->scanstatus == SUBQUERY_SCAN_UNKNOWN);
1540 /* Initially, mark the SubqueryScan as non-deletable from the plan tree */
1541 plan->scanstatus = SUBQUERY_SCAN_NONTRIVIAL;
1542
1543 if (plan->scan.plan.qual != NIL)
1544 return false;
1545
1546 if (list_length(plan->scan.plan.targetlist) !=
1547 list_length(plan->subplan->targetlist))
1548 return false; /* tlists not same length */
1549
1550 attrno = 1;
1551 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1552 {
1555
1556 if (ptle->resjunk != ctle->resjunk)
1557 return false; /* tlist doesn't match junk status */
1558
1559 /*
1560 * We accept either a Var referencing the corresponding element of the
1561 * subplan tlist, or a Const equaling the subplan element. See
1562 * generate_setop_tlist() for motivation.
1563 */
1564 if (ptle->expr && IsA(ptle->expr, Var))
1565 {
1566 Var *var = (Var *) ptle->expr;
1567
1568 Assert(var->varno == plan->scan.scanrelid);
1569 Assert(var->varlevelsup == 0);
1570 if (var->varattno != attrno)
1571 return false; /* out of order */
1572 }
1573 else if (ptle->expr && IsA(ptle->expr, Const))
1574 {
1575 if (!equal(ptle->expr, ctle->expr))
1576 return false;
1577 }
1578 else
1579 return false;
1580
1581 attrno++;
1582 }
1583
1584 /* Re-mark the SubqueryScan as deletable from the plan tree */
1585 plan->scanstatus = SUBQUERY_SCAN_TRIVIAL;
1586
1587 return true;
1588}
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
@ SUBQUERY_SCAN_NONTRIVIAL
Definition plannodes.h:770
@ SUBQUERY_SCAN_UNKNOWN
Definition plannodes.h:768
@ SUBQUERY_SCAN_TRIVIAL
Definition plannodes.h:769

References Assert, equal(), fb(), 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 68 of file planner.c.

Referenced by standard_planner().

◆ enable_self_join_elimination

PGDLLIMPORT bool enable_self_join_elimination
extern

Definition at line 54 of file analyzejoins.c.

Referenced by remove_useless_self_joins().

◆ from_collapse_limit

PGDLLIMPORT int from_collapse_limit
extern

Definition at line 41 of file initsplan.c.

Referenced by deconstruct_recurse().

◆ join_collapse_limit

PGDLLIMPORT int join_collapse_limit
extern

Definition at line 42 of file initsplan.c.

Referenced by deconstruct_recurse().