PostgreSQL Source Code  git master
allpaths.c File Reference
#include "postgres.h"
#include <limits.h>
#include <math.h>
#include "access/sysattr.h"
#include "access/tsmapi.h"
#include "catalog/pg_class.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "foreign/fdwapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/inherit.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/planner.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "partitioning/partbounds.h"
#include "partitioning/partprune.h"
#include "rewrite/rewriteManip.h"
#include "utils/lsyscache.h"
Include dependency graph for allpaths.c:

Go to the source code of this file.

Data Structures

struct  pushdown_safety_info
 

Typedefs

typedef struct pushdown_safety_info pushdown_safety_info
 

Functions

static void set_base_rel_consider_startup (PlannerInfo *root)
 
static void set_base_rel_sizes (PlannerInfo *root)
 
static void set_base_rel_pathlists (PlannerInfo *root)
 
static void set_rel_size (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_plain_rel_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void create_plain_partial_paths (PlannerInfo *root, RelOptInfo *rel)
 
static void set_rel_consider_parallel (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_plain_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablesample_rel_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablesample_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_foreign_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_foreign_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_append_rel_size (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_append_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void generate_orderedappend_paths (PlannerInfo *root, RelOptInfo *rel, List *live_childrels, List *all_child_pathkeys, List *partitioned_rels)
 
static Pathget_cheapest_parameterized_child_path (PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
 
static Listaccumulate_partitioned_rels (List *partitioned_rels, List *sub_partitioned_rels, bool flatten_partitioned_rels)
 
static void accumulate_append_subpath (Path *path, List **subpaths, List **special_subpaths, List **partitioned_rels, bool flatten_partitioned_rels)
 
static Pathget_singleton_append_subpath (Path *path)
 
static void set_dummy_rel_pathlist (RelOptInfo *rel)
 
static void set_subquery_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_function_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_values_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablefunc_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_cte_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_namedtuplestore_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_result_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_worktable_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static RelOptInfomake_rel_from_joinlist (PlannerInfo *root, List *joinlist)
 
static bool subquery_is_pushdown_safe (Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
 
static bool recurse_pushdown_safe (Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
 
static void check_output_expressions (Query *subquery, pushdown_safety_info *safetyInfo)
 
static void compare_tlist_datatypes (List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
 
static bool targetIsInAllPartitionLists (TargetEntry *tle, Query *query)
 
static bool qual_is_pushdown_safe (Query *subquery, Index rti, Node *qual, pushdown_safety_info *safetyInfo)
 
static void subquery_push_qual (Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
 
static void recurse_push_qual (Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
 
static void remove_unused_subquery_outputs (Query *subquery, RelOptInfo *rel)
 
RelOptInfomake_one_rel (PlannerInfo *root, List *joinlist)
 
void add_paths_to_append_rel (PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
 
static bool has_multiple_baserels (PlannerInfo *root)
 
void generate_gather_paths (PlannerInfo *root, RelOptInfo *rel, bool override_rows)
 
static Listget_useful_pathkeys_for_relation (PlannerInfo *root, RelOptInfo *rel, bool require_parallel_safe)
 
void generate_useful_gather_paths (PlannerInfo *root, RelOptInfo *rel, bool override_rows)
 
RelOptInfostandard_join_search (PlannerInfo *root, int levels_needed, List *initial_rels)
 
void create_partial_bitmap_paths (PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual)
 
int compute_parallel_worker (RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
 
void generate_partitionwise_join_paths (PlannerInfo *root, RelOptInfo *rel)
 

Variables

bool enable_geqo = false
 
int geqo_threshold
 
int min_parallel_table_scan_size
 
int min_parallel_index_scan_size
 
set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL
 
join_search_hook_type join_search_hook = NULL
 

Typedef Documentation

◆ pushdown_safety_info

Function Documentation

◆ accumulate_append_subpath()

static void accumulate_append_subpath ( Path path,
List **  subpaths,
List **  special_subpaths,
List **  partitioned_rels,
bool  flatten_partitioned_rels 
)
static

Definition at line 2094 of file allpaths.c.

References accumulate_partitioned_rels(), AppendPath::first_partial_path, IsA, lappend(), list_concat(), list_copy(), list_copy_tail(), list_truncate(), Path::parallel_aware, AppendPath::partitioned_rels, MergeAppendPath::partitioned_rels, AppendPath::path, AppendPath::subpaths, and MergeAppendPath::subpaths.

Referenced by add_paths_to_append_rel(), and generate_orderedappend_paths().

2097 {
2098  if (IsA(path, AppendPath))
2099  {
2100  AppendPath *apath = (AppendPath *) path;
2101 
2102  if (!apath->path.parallel_aware || apath->first_partial_path == 0)
2103  {
2104  *subpaths = list_concat(*subpaths, apath->subpaths);
2105  *partitioned_rels = accumulate_partitioned_rels(*partitioned_rels,
2106  apath->partitioned_rels,
2107  flatten_partitioned_rels);
2108  return;
2109  }
2110  else if (special_subpaths != NULL)
2111  {
2112  List *new_special_subpaths;
2113 
2114  /* Split Parallel Append into partial and non-partial subpaths */
2115  *subpaths = list_concat(*subpaths,
2116  list_copy_tail(apath->subpaths,
2117  apath->first_partial_path));
2118  new_special_subpaths =
2120  apath->first_partial_path);
2121  *special_subpaths = list_concat(*special_subpaths,
2122  new_special_subpaths);
2123  *partitioned_rels = accumulate_partitioned_rels(*partitioned_rels,
2124  apath->partitioned_rels,
2125  flatten_partitioned_rels);
2126  return;
2127  }
2128  }
2129  else if (IsA(path, MergeAppendPath))
2130  {
2131  MergeAppendPath *mpath = (MergeAppendPath *) path;
2132 
2133  *subpaths = list_concat(*subpaths, mpath->subpaths);
2134  *partitioned_rels = accumulate_partitioned_rels(*partitioned_rels,
2135  mpath->partitioned_rels,
2136  flatten_partitioned_rels);
2137  return;
2138  }
2139 
2140  *subpaths = lappend(*subpaths, path);
2141 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
List * list_truncate(List *list, int new_size)
Definition: list.c:600
List * list_copy(const List *oldlist)
Definition: list.c:1418
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1437
List * partitioned_rels
Definition: pathnodes.h:1433
int first_partial_path
Definition: pathnodes.h:1411
List * subpaths
Definition: pathnodes.h:1409
List * lappend(List *list, void *datum)
Definition: list.c:336
static List * accumulate_partitioned_rels(List *partitioned_rels, List *sub_partitioned_rels, bool flatten_partitioned_rels)
Definition: allpaths.c:2017
List * partitioned_rels
Definition: pathnodes.h:1406
bool parallel_aware
Definition: pathnodes.h:1153
Definition: pg_list.h:50

◆ accumulate_partitioned_rels()

static List * accumulate_partitioned_rels ( List partitioned_rels,
List sub_partitioned_rels,
bool  flatten_partitioned_rels 
)
static

Definition at line 2017 of file allpaths.c.

References Assert, bms_add_members(), linitial, list_concat(), list_length(), and NIL.

Referenced by accumulate_append_subpath().

2020 {
2021  if (flatten)
2022  {
2023  /*
2024  * We're only called with flatten == true when the partitioned_rels
2025  * list has at most 1 element. So we can just add the members from
2026  * sub list's first element onto the first element of
2027  * partitioned_rels. Only later in planning when doing UNION ALL
2028  * Append processing will we see flatten == false. partitioned_rels
2029  * may end up with more than 1 element then, but we never expect to be
2030  * called with flatten == true again after that, so we needn't bother
2031  * doing anything here for anything but the initial element.
2032  */
2033  if (partitioned_rels != NIL && sub_partitioned_rels != NIL)
2034  {
2035  Relids partrels = (Relids) linitial(partitioned_rels);
2036  Relids subpartrels = (Relids) linitial(sub_partitioned_rels);
2037 
2038  /* Ensure the above comment holds true */
2039  Assert(list_length(partitioned_rels) == 1);
2040  Assert(list_length(sub_partitioned_rels) == 1);
2041 
2042  linitial(partitioned_rels) = bms_add_members(partrels, subpartrels);
2043  }
2044  }
2045  else
2046  {
2047  /*
2048  * Handle UNION ALL to partitioned tables. This always occurs after
2049  * we've done the accumulation for sub-partitioned tables, so there's
2050  * no need to consider how adding multiple elements to the top level
2051  * list affects the flatten == true case above.
2052  */
2053  partitioned_rels = list_concat(partitioned_rels, sub_partitioned_rels);
2054  }
2055 
2056  return partitioned_rels;
2057 }
#define NIL
Definition: pg_list.h:65
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
#define linitial(l)
Definition: pg_list.h:174
#define Assert(condition)
Definition: c.h:792
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * Relids
Definition: pathnodes.h:28
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ add_paths_to_append_rel()

void add_paths_to_append_rel ( PlannerInfo root,
RelOptInfo rel,
List live_childrels 
)

Definition at line 1289 of file allpaths.c.

References accumulate_append_subpath(), add_partial_path(), add_path(), Assert, bms_equal(), bms_make_singleton(), RelOptInfo::cheapest_total_path, compare_pathkeys(), RelOptInfo::consider_parallel, create_append_path(), enable_parallel_append, fls(), for_each_from, generate_orderedappend_paths(), get_cheapest_parallel_safe_total_inner(), get_cheapest_parameterized_child_path(), lappend(), lfirst, linitial, list_length(), list_make1, Max, max_parallel_workers_per_gather, Min, NIL, Path::parallel_workers, Path::param_info, RelOptInfo::part_scheme, RelOptInfo::partial_pathlist, AppendPath::path, PATH_REQ_OUTER, Path::pathkeys, PATHKEYS_EQUAL, RelOptInfo::pathlist, RelOptInfo::relid, RELOPT_JOINREL, RelOptInfo::reloptkind, Path::rows, RTE_SUBQUERY, RelOptInfo::rtekind, subpath(), and Path::total_cost.

Referenced by apply_scanjoin_target_to_paths(), create_partitionwise_grouping_paths(), generate_partitionwise_join_paths(), and set_append_rel_pathlist().

1291 {
1292  List *subpaths = NIL;
1293  bool subpaths_valid = true;
1294  List *partial_subpaths = NIL;
1295  List *pa_partial_subpaths = NIL;
1296  List *pa_nonpartial_subpaths = NIL;
1297  bool partial_subpaths_valid = true;
1298  bool pa_subpaths_valid;
1299  List *all_child_pathkeys = NIL;
1300  List *all_child_outers = NIL;
1301  ListCell *l;
1302  List *partitioned_rels = NIL;
1303  List *partial_partitioned_rels = NIL;
1304  List *pa_partitioned_rels = NIL;
1305  double partial_rows = -1;
1306  bool flatten_partitioned_rels;
1307 
1308  /* If appropriate, consider parallel append */
1309  pa_subpaths_valid = enable_parallel_append && rel->consider_parallel;
1310 
1311  /* What we do with the partitioned_rels list is different for UNION ALL */
1312  flatten_partitioned_rels = (rel->rtekind != RTE_SUBQUERY);
1313 
1314  /*
1315  * For partitioned tables, we accumulate a list of Relids of each
1316  * partitioned table which has at least one of its subpartitions directly
1317  * present as a subpath in this Append. This is used later for run-time
1318  * partition pruning. We must maintain separate lists for each Append
1319  * Path that we create as some paths that we create here can't flatten
1320  * sub-Appends and sub-MergeAppends into the top-level Append. We needn't
1321  * bother doing this for join rels as no run-time pruning is done on
1322  * those.
1323  */
1324  if (rel->reloptkind != RELOPT_JOINREL && rel->part_scheme != NULL)
1325  {
1326  partitioned_rels = list_make1(bms_make_singleton(rel->relid));
1327  partial_partitioned_rels = list_make1(bms_make_singleton(rel->relid));
1328 
1329  /* skip this one if we're not going to make a Parallel Append path */
1330  if (pa_subpaths_valid)
1331  pa_partitioned_rels = list_make1(bms_make_singleton(rel->relid));
1332  }
1333 
1334  /*
1335  * For every non-dummy child, remember the cheapest path. Also, identify
1336  * all pathkeys (orderings) and parameterizations (required_outer sets)
1337  * available for the non-dummy member relations.
1338  */
1339  foreach(l, live_childrels)
1340  {
1341  RelOptInfo *childrel = lfirst(l);
1342  ListCell *lcp;
1343  Path *cheapest_partial_path = NULL;
1344 
1345  /*
1346  * If child has an unparameterized cheapest-total path, add that to
1347  * the unparameterized Append path we are constructing for the parent.
1348  * If not, there's no workable unparameterized path.
1349  *
1350  * With partitionwise aggregates, the child rel's pathlist may be
1351  * empty, so don't assume that a path exists here.
1352  */
1353  if (childrel->pathlist != NIL &&
1354  childrel->cheapest_total_path->param_info == NULL)
1356  &subpaths, NULL, &partitioned_rels,
1357  flatten_partitioned_rels);
1358  else
1359  subpaths_valid = false;
1360 
1361  /* Same idea, but for a partial plan. */
1362  if (childrel->partial_pathlist != NIL)
1363  {
1364  cheapest_partial_path = linitial(childrel->partial_pathlist);
1365  accumulate_append_subpath(cheapest_partial_path,
1366  &partial_subpaths, NULL,
1367  &partial_partitioned_rels,
1368  flatten_partitioned_rels);
1369  }
1370  else
1371  partial_subpaths_valid = false;
1372 
1373  /*
1374  * Same idea, but for a parallel append mixing partial and non-partial
1375  * paths.
1376  */
1377  if (pa_subpaths_valid)
1378  {
1379  Path *nppath = NULL;
1380 
1381  nppath =
1383 
1384  if (cheapest_partial_path == NULL && nppath == NULL)
1385  {
1386  /* Neither a partial nor a parallel-safe path? Forget it. */
1387  pa_subpaths_valid = false;
1388  }
1389  else if (nppath == NULL ||
1390  (cheapest_partial_path != NULL &&
1391  cheapest_partial_path->total_cost < nppath->total_cost))
1392  {
1393  /* Partial path is cheaper or the only option. */
1394  Assert(cheapest_partial_path != NULL);
1395  accumulate_append_subpath(cheapest_partial_path,
1396  &pa_partial_subpaths,
1397  &pa_nonpartial_subpaths,
1398  &pa_partitioned_rels,
1399  flatten_partitioned_rels);
1400 
1401  }
1402  else
1403  {
1404  /*
1405  * Either we've got only a non-partial path, or we think that
1406  * a single backend can execute the best non-partial path
1407  * faster than all the parallel backends working together can
1408  * execute the best partial path.
1409  *
1410  * It might make sense to be more aggressive here. Even if
1411  * the best non-partial path is more expensive than the best
1412  * partial path, it could still be better to choose the
1413  * non-partial path if there are several such paths that can
1414  * be given to different workers. For now, we don't try to
1415  * figure that out.
1416  */
1418  &pa_nonpartial_subpaths,
1419  NULL,
1420  &pa_partitioned_rels,
1421  flatten_partitioned_rels);
1422  }
1423  }
1424 
1425  /*
1426  * Collect lists of all the available path orderings and
1427  * parameterizations for all the children. We use these as a
1428  * heuristic to indicate which sort orderings and parameterizations we
1429  * should build Append and MergeAppend paths for.
1430  */
1431  foreach(lcp, childrel->pathlist)
1432  {
1433  Path *childpath = (Path *) lfirst(lcp);
1434  List *childkeys = childpath->pathkeys;
1435  Relids childouter = PATH_REQ_OUTER(childpath);
1436 
1437  /* Unsorted paths don't contribute to pathkey list */
1438  if (childkeys != NIL)
1439  {
1440  ListCell *lpk;
1441  bool found = false;
1442 
1443  /* Have we already seen this ordering? */
1444  foreach(lpk, all_child_pathkeys)
1445  {
1446  List *existing_pathkeys = (List *) lfirst(lpk);
1447 
1448  if (compare_pathkeys(existing_pathkeys,
1449  childkeys) == PATHKEYS_EQUAL)
1450  {
1451  found = true;
1452  break;
1453  }
1454  }
1455  if (!found)
1456  {
1457  /* No, so add it to all_child_pathkeys */
1458  all_child_pathkeys = lappend(all_child_pathkeys,
1459  childkeys);
1460  }
1461  }
1462 
1463  /* Unparameterized paths don't contribute to param-set list */
1464  if (childouter)
1465  {
1466  ListCell *lco;
1467  bool found = false;
1468 
1469  /* Have we already seen this param set? */
1470  foreach(lco, all_child_outers)
1471  {
1472  Relids existing_outers = (Relids) lfirst(lco);
1473 
1474  if (bms_equal(existing_outers, childouter))
1475  {
1476  found = true;
1477  break;
1478  }
1479  }
1480  if (!found)
1481  {
1482  /* No, so add it to all_child_outers */
1483  all_child_outers = lappend(all_child_outers,
1484  childouter);
1485  }
1486  }
1487  }
1488  }
1489 
1490  /*
1491  * If we found unparameterized paths for all children, build an unordered,
1492  * unparameterized Append path for the rel. (Note: this is correct even
1493  * if we have zero or one live subpath due to constraint exclusion.)
1494  */
1495  if (subpaths_valid)
1496  add_path(rel, (Path *) create_append_path(root, rel, subpaths, NIL,
1497  NIL, NULL, 0, false,
1498  partitioned_rels, -1));
1499 
1500  /*
1501  * Consider an append of unordered, unparameterized partial paths. Make
1502  * it parallel-aware if possible.
1503  */
1504  if (partial_subpaths_valid && partial_subpaths != NIL)
1505  {
1506  AppendPath *appendpath;
1507  ListCell *lc;
1508  int parallel_workers = 0;
1509 
1510  /* Find the highest number of workers requested for any subpath. */
1511  foreach(lc, partial_subpaths)
1512  {
1513  Path *path = lfirst(lc);
1514 
1515  parallel_workers = Max(parallel_workers, path->parallel_workers);
1516  }
1517  Assert(parallel_workers > 0);
1518 
1519  /*
1520  * If the use of parallel append is permitted, always request at least
1521  * log2(# of children) workers. We assume it can be useful to have
1522  * extra workers in this case because they will be spread out across
1523  * the children. The precise formula is just a guess, but we don't
1524  * want to end up with a radically different answer for a table with N
1525  * partitions vs. an unpartitioned table with the same data, so the
1526  * use of some kind of log-scaling here seems to make some sense.
1527  */
1529  {
1530  parallel_workers = Max(parallel_workers,
1531  fls(list_length(live_childrels)));
1532  parallel_workers = Min(parallel_workers,
1534  }
1535  Assert(parallel_workers > 0);
1536 
1537  /* Generate a partial append path. */
1538  appendpath = create_append_path(root, rel, NIL, partial_subpaths,
1539  NIL, NULL, parallel_workers,
1541  partial_partitioned_rels, -1);
1542 
1543  /*
1544  * Make sure any subsequent partial paths use the same row count
1545  * estimate.
1546  */
1547  partial_rows = appendpath->path.rows;
1548 
1549  /* Add the path. */
1550  add_partial_path(rel, (Path *) appendpath);
1551  }
1552 
1553  /*
1554  * Consider a parallel-aware append using a mix of partial and non-partial
1555  * paths. (This only makes sense if there's at least one child which has
1556  * a non-partial path that is substantially cheaper than any partial path;
1557  * otherwise, we should use the append path added in the previous step.)
1558  */
1559  if (pa_subpaths_valid && pa_nonpartial_subpaths != NIL)
1560  {
1561  AppendPath *appendpath;
1562  ListCell *lc;
1563  int parallel_workers = 0;
1564 
1565  /*
1566  * Find the highest number of workers requested for any partial
1567  * subpath.
1568  */
1569  foreach(lc, pa_partial_subpaths)
1570  {
1571  Path *path = lfirst(lc);
1572 
1573  parallel_workers = Max(parallel_workers, path->parallel_workers);
1574  }
1575 
1576  /*
1577  * Same formula here as above. It's even more important in this
1578  * instance because the non-partial paths won't contribute anything to
1579  * the planned number of parallel workers.
1580  */
1581  parallel_workers = Max(parallel_workers,
1582  fls(list_length(live_childrels)));
1583  parallel_workers = Min(parallel_workers,
1585  Assert(parallel_workers > 0);
1586 
1587  appendpath = create_append_path(root, rel, pa_nonpartial_subpaths,
1588  pa_partial_subpaths,
1589  NIL, NULL, parallel_workers, true,
1590  pa_partitioned_rels, partial_rows);
1591  add_partial_path(rel, (Path *) appendpath);
1592  }
1593 
1594  /*
1595  * Also build unparameterized ordered append paths based on the collected
1596  * list of child pathkeys.
1597  */
1598  if (subpaths_valid)
1599  generate_orderedappend_paths(root, rel, live_childrels,
1600  all_child_pathkeys,
1601  partitioned_rels);
1602 
1603  /*
1604  * Build Append paths for each parameterization seen among the child rels.
1605  * (This may look pretty expensive, but in most cases of practical
1606  * interest, the child rels will expose mostly the same parameterizations,
1607  * so that not that many cases actually get considered here.)
1608  *
1609  * The Append node itself cannot enforce quals, so all qual checking must
1610  * be done in the child paths. This means that to have a parameterized
1611  * Append path, we must have the exact same parameterization for each
1612  * child path; otherwise some children might be failing to check the
1613  * moved-down quals. To make them match up, we can try to increase the
1614  * parameterization of lesser-parameterized paths.
1615  */
1616  foreach(l, all_child_outers)
1617  {
1618  Relids required_outer = (Relids) lfirst(l);
1619  ListCell *lcr;
1620  List *part_rels = NIL;
1621 
1622  if (rel->reloptkind != RELOPT_JOINREL && rel->part_scheme != NULL)
1623  part_rels = list_make1(bms_make_singleton(rel->relid));
1624 
1625  /* Select the child paths for an Append with this parameterization */
1626  subpaths = NIL;
1627  subpaths_valid = true;
1628  foreach(lcr, live_childrels)
1629  {
1630  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1631  Path *subpath;
1632 
1633  if (childrel->pathlist == NIL)
1634  {
1635  /* failed to make a suitable path for this child */
1636  subpaths_valid = false;
1637  break;
1638  }
1639 
1641  childrel,
1642  required_outer);
1643  if (subpath == NULL)
1644  {
1645  /* failed to make a suitable path for this child */
1646  subpaths_valid = false;
1647  break;
1648  }
1649  accumulate_append_subpath(subpath, &subpaths, NULL, &part_rels,
1650  flatten_partitioned_rels);
1651  }
1652 
1653  if (subpaths_valid)
1654  add_path(rel, (Path *)
1655  create_append_path(root, rel, subpaths, NIL,
1656  NIL, required_outer, 0, false,
1657  part_rels, -1));
1658  }
1659 
1660  /*
1661  * When there is only a single child relation, the Append path can inherit
1662  * any ordering available for the child rel's path, so that it's useful to
1663  * consider ordered partial paths. Above we only considered the cheapest
1664  * partial path for each child, but let's also make paths using any
1665  * partial paths that have pathkeys.
1666  */
1667  if (list_length(live_childrels) == 1)
1668  {
1669  RelOptInfo *childrel = (RelOptInfo *) linitial(live_childrels);
1670 
1671  /* skip the cheapest partial path, since we already used that above */
1672  for_each_from(l, childrel->partial_pathlist, 1)
1673  {
1674  Path *path = (Path *) lfirst(l);
1675  AppendPath *appendpath;
1676 
1677  /* skip paths with no pathkeys. */
1678  if (path->pathkeys == NIL)
1679  continue;
1680 
1681  appendpath = create_append_path(root, rel, NIL, list_make1(path),
1682  NIL, NULL,
1683  path->parallel_workers, true,
1684  partitioned_rels, partial_rows);
1685  add_partial_path(rel, (Path *) appendpath);
1686  }
1687  }
1688 }
#define NIL
Definition: pg_list.h:65
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
RelOptKind reloptkind
Definition: pathnodes.h:663
static Path * get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: allpaths.c:1945
static void accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths, List **partitioned_rels, bool flatten_partitioned_rels)
Definition: allpaths.c:2094
#define Min(x, y)
Definition: c.h:974
int parallel_workers
Definition: pathnodes.h:1155
ParamPathInfo * param_info
Definition: pathnodes.h:1151
List * partial_pathlist
Definition: pathnodes.h:682
bool enable_parallel_append
Definition: costsize.c:147
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:285
int fls(int mask)
Definition: fls.c:55
#define list_make1(x1)
Definition: pg_list.h:206
#define linitial(l)
Definition: pg_list.h:174
struct Path * cheapest_total_path
Definition: pathnodes.h:684
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1167
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
Path * get_cheapest_parallel_safe_total_inner(List *paths)
Definition: pathkeys.c:482
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, List *partitioned_rels, double rows)
Definition: pathnode.c:1215
Index relid
Definition: pathnodes.h:694
List * lappend(List *list, void *datum)
Definition: list.c:336
RTEKind rtekind
Definition: pathnodes.h:696
Cost total_cost
Definition: pathnodes.h:1160
List * pathkeys
Definition: pathnodes.h:1162
#define Max(x, y)
Definition: c.h:968
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
double rows
Definition: pathnodes.h:1158
static int list_length(const List *l)
Definition: pg_list.h:149
#define for_each_from(cell, lst, N)
Definition: pg_list.h:393
bool consider_parallel
Definition: pathnodes.h:674
Bitmapset * Relids
Definition: pathnodes.h:28
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:749
PartitionScheme part_scheme
Definition: pathnodes.h:743
List * pathlist
Definition: pathnodes.h:680
static void generate_orderedappend_paths(PlannerInfo *root, RelOptInfo *rel, List *live_childrels, List *all_child_pathkeys, List *partitioned_rels)
Definition: allpaths.c:1718
int max_parallel_workers_per_gather
Definition: costsize.c:130
Definition: pg_list.h:50
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ check_output_expressions()

static void check_output_expressions ( Query subquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3415 of file allpaths.c.

References contain_volatile_functions(), Query::distinctClause, TargetEntry::expr, expression_returns_set(), Query::hasDistinctOn, Query::hasTargetSRFs, Query::hasWindowFuncs, InvalidOid, lfirst, TargetEntry::resjunk, TargetEntry::resno, targetIsInAllPartitionLists(), targetIsInSortList(), Query::targetList, and pushdown_safety_info::unsafeColumns.

Referenced by subquery_is_pushdown_safe().

3416 {
3417  ListCell *lc;
3418 
3419  foreach(lc, subquery->targetList)
3420  {
3421  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3422 
3423  if (tle->resjunk)
3424  continue; /* ignore resjunk columns */
3425 
3426  /* We need not check further if output col is already known unsafe */
3427  if (safetyInfo->unsafeColumns[tle->resno])
3428  continue;
3429 
3430  /* Functions returning sets are unsafe (point 1) */
3431  if (subquery->hasTargetSRFs &&
3432  expression_returns_set((Node *) tle->expr))
3433  {
3434  safetyInfo->unsafeColumns[tle->resno] = true;
3435  continue;
3436  }
3437 
3438  /* Volatile functions are unsafe (point 2) */
3439  if (contain_volatile_functions((Node *) tle->expr))
3440  {
3441  safetyInfo->unsafeColumns[tle->resno] = true;
3442  continue;
3443  }
3444 
3445  /* If subquery uses DISTINCT ON, check point 3 */
3446  if (subquery->hasDistinctOn &&
3447  !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
3448  {
3449  /* non-DISTINCT column, so mark it unsafe */
3450  safetyInfo->unsafeColumns[tle->resno] = true;
3451  continue;
3452  }
3453 
3454  /* If subquery uses window functions, check point 4 */
3455  if (subquery->hasWindowFuncs &&
3456  !targetIsInAllPartitionLists(tle, subquery))
3457  {
3458  /* not present in all PARTITION BY clauses, so mark it unsafe */
3459  safetyInfo->unsafeColumns[tle->resno] = true;
3460  continue;
3461  }
3462  }
3463 }
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:709
Definition: nodes.h:528
bool * unsafeColumns
Definition: allpaths.c:56
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:437
bool hasDistinctOn
Definition: parsenodes.h:129
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1438
List * distinctClause
Definition: parsenodes.h:156
static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
Definition: allpaths.c:3516
AttrNumber resno
Definition: primnodes.h:1432
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
bool hasTargetSRFs
Definition: parsenodes.h:127
#define lfirst(lc)
Definition: pg_list.h:169
bool hasWindowFuncs
Definition: parsenodes.h:126
Expr * expr
Definition: primnodes.h:1431

◆ compare_tlist_datatypes()

static void compare_tlist_datatypes ( List tlist,
List colTypes,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3483 of file allpaths.c.

References elog, ERROR, TargetEntry::expr, exprType(), lfirst, lfirst_oid, list_head(), lnext(), TargetEntry::resjunk, TargetEntry::resno, and pushdown_safety_info::unsafeColumns.

Referenced by subquery_is_pushdown_safe().

3485 {
3486  ListCell *l;
3487  ListCell *colType = list_head(colTypes);
3488 
3489  foreach(l, tlist)
3490  {
3491  TargetEntry *tle = (TargetEntry *) lfirst(l);
3492 
3493  if (tle->resjunk)
3494  continue; /* ignore resjunk columns */
3495  if (colType == NULL)
3496  elog(ERROR, "wrong number of tlist entries");
3497  if (exprType((Node *) tle->expr) != lfirst_oid(colType))
3498  safetyInfo->unsafeColumns[tle->resno] = true;
3499  colType = lnext(colTypes, colType);
3500  }
3501  if (colType != NULL)
3502  elog(ERROR, "wrong number of tlist entries");
3503 }
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
Definition: nodes.h:528
bool * unsafeColumns
Definition: allpaths.c:56
bool resjunk
Definition: primnodes.h:1438
#define ERROR
Definition: elog.h:45
AttrNumber resno
Definition: primnodes.h:1432
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1431
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
#define elog(elevel,...)
Definition: elog.h:228
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ compute_parallel_worker()

int compute_parallel_worker ( RelOptInfo rel,
double  heap_pages,
double  index_pages,
int  max_workers 
)

Definition at line 3884 of file allpaths.c.

References Max, Min, min_parallel_index_scan_size, min_parallel_table_scan_size, RelOptInfo::rel_parallel_workers, RELOPT_BASEREL, and RelOptInfo::reloptkind.

Referenced by cost_index(), create_partial_bitmap_paths(), create_plain_partial_paths(), and plan_create_index_workers().

3886 {
3887  int parallel_workers = 0;
3888 
3889  /*
3890  * If the user has set the parallel_workers reloption, use that; otherwise
3891  * select a default number of workers.
3892  */
3893  if (rel->rel_parallel_workers != -1)
3894  parallel_workers = rel->rel_parallel_workers;
3895  else
3896  {
3897  /*
3898  * If the number of pages being scanned is insufficient to justify a
3899  * parallel scan, just return zero ... unless it's an inheritance
3900  * child. In that case, we want to generate a parallel path here
3901  * anyway. It might not be worthwhile just for this relation, but
3902  * when combined with all of its inheritance siblings it may well pay
3903  * off.
3904  */
3905  if (rel->reloptkind == RELOPT_BASEREL &&
3906  ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3907  (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3908  return 0;
3909 
3910  if (heap_pages >= 0)
3911  {
3912  int heap_parallel_threshold;
3913  int heap_parallel_workers = 1;
3914 
3915  /*
3916  * Select the number of workers based on the log of the size of
3917  * the relation. This probably needs to be a good deal more
3918  * sophisticated, but we need something here for now. Note that
3919  * the upper limit of the min_parallel_table_scan_size GUC is
3920  * chosen to prevent overflow here.
3921  */
3922  heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
3923  while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
3924  {
3925  heap_parallel_workers++;
3926  heap_parallel_threshold *= 3;
3927  if (heap_parallel_threshold > INT_MAX / 3)
3928  break; /* avoid overflow */
3929  }
3930 
3931  parallel_workers = heap_parallel_workers;
3932  }
3933 
3934  if (index_pages >= 0)
3935  {
3936  int index_parallel_workers = 1;
3937  int index_parallel_threshold;
3938 
3939  /* same calculation as for heap_pages above */
3940  index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
3941  while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
3942  {
3943  index_parallel_workers++;
3944  index_parallel_threshold *= 3;
3945  if (index_parallel_threshold > INT_MAX / 3)
3946  break; /* avoid overflow */
3947  }
3948 
3949  if (parallel_workers > 0)
3950  parallel_workers = Min(parallel_workers, index_parallel_workers);
3951  else
3952  parallel_workers = index_parallel_workers;
3953  }
3954  }
3955 
3956  /* In no case use more than caller supplied maximum number of workers */
3957  parallel_workers = Min(parallel_workers, max_workers);
3958 
3959  return parallel_workers;
3960 }
RelOptKind reloptkind
Definition: pathnodes.h:663
#define Min(x, y)
Definition: c.h:974
uint32 BlockNumber
Definition: block.h:31
int min_parallel_index_scan_size
Definition: allpaths.c:65
int rel_parallel_workers
Definition: pathnodes.h:712
#define Max(x, y)
Definition: c.h:968
int min_parallel_table_scan_size
Definition: allpaths.c:64

◆ create_partial_bitmap_paths()

void create_partial_bitmap_paths ( PlannerInfo root,
RelOptInfo rel,
Path bitmapqual 
)

Definition at line 3848 of file allpaths.c.

References add_partial_path(), compute_bitmap_pages(), compute_parallel_worker(), create_bitmap_heap_path(), RelOptInfo::lateral_relids, and max_parallel_workers_per_gather.

Referenced by create_index_paths().

3850 {
3851  int parallel_workers;
3852  double pages_fetched;
3853 
3854  /* Compute heap pages for bitmap heap scan */
3855  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
3856  NULL, NULL);
3857 
3858  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1,
3860 
3861  if (parallel_workers <= 0)
3862  return;
3863 
3864  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3865  bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3866 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:3884
Relids lateral_relids
Definition: pathnodes.h:691
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:749
int max_parallel_workers_per_gather
Definition: costsize.c:130
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:5802
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1046

◆ create_plain_partial_paths()

static void create_plain_partial_paths ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 801 of file allpaths.c.

References add_partial_path(), compute_parallel_worker(), create_seqscan_path(), max_parallel_workers_per_gather, and RelOptInfo::pages.

Referenced by set_plain_rel_pathlist().

802 {
803  int parallel_workers;
804 
805  parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
807 
808  /* If any limit was set to zero, the user doesn't want a parallel scan. */
809  if (parallel_workers <= 0)
810  return;
811 
812  /* Add an unordered partial path based on a parallel sequential scan. */
813  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
814 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:3884
BlockNumber pages
Definition: pathnodes.h:705
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:749
int max_parallel_workers_per_gather
Definition: costsize.c:130
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:929

◆ generate_gather_paths()

void generate_gather_paths ( PlannerInfo root,
RelOptInfo rel,
bool  override_rows 
)

Definition at line 2745 of file allpaths.c.

References add_path(), create_gather_merge_path(), create_gather_path(), lfirst, linitial, NIL, Path::parallel_workers, RelOptInfo::partial_pathlist, GatherMergePath::path, Path::pathkeys, RelOptInfo::reltarget, Path::rows, and subpath().

Referenced by generate_useful_gather_paths().

2746 {
2747  Path *cheapest_partial_path;
2748  Path *simple_gather_path;
2749  ListCell *lc;
2750  double rows;
2751  double *rowsp = NULL;
2752 
2753  /* If there are no partial paths, there's nothing to do here. */
2754  if (rel->partial_pathlist == NIL)
2755  return;
2756 
2757  /* Should we override the rel's rowcount estimate? */
2758  if (override_rows)
2759  rowsp = &rows;
2760 
2761  /*
2762  * The output of Gather is always unsorted, so there's only one partial
2763  * path of interest: the cheapest one. That will be the one at the front
2764  * of partial_pathlist because of the way add_partial_path works.
2765  */
2766  cheapest_partial_path = linitial(rel->partial_pathlist);
2767  rows =
2768  cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
2769  simple_gather_path = (Path *)
2770  create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2771  NULL, rowsp);
2772  add_path(rel, simple_gather_path);
2773 
2774  /*
2775  * For each useful ordering, we can consider an order-preserving Gather
2776  * Merge.
2777  */
2778  foreach(lc, rel->partial_pathlist)
2779  {
2780  Path *subpath = (Path *) lfirst(lc);
2781  GatherMergePath *path;
2782 
2783  if (subpath->pathkeys == NIL)
2784  continue;
2785 
2786  rows = subpath->rows * subpath->parallel_workers;
2787  path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2788  subpath->pathkeys, NULL, rowsp);
2789  add_path(rel, &path->path);
2790  }
2791 }
#define NIL
Definition: pg_list.h:65
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
Definition: pathnode.c:1878
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
int parallel_workers
Definition: pathnodes.h:1155
List * partial_pathlist
Definition: pathnodes.h:682
#define linitial(l)
Definition: pg_list.h:174
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1787
List * pathkeys
Definition: pathnodes.h:1162
#define lfirst(lc)
Definition: pg_list.h:169
double rows
Definition: pathnodes.h:1158
struct PathTarget * reltarget
Definition: pathnodes.h:677
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241

◆ generate_orderedappend_paths()

static void generate_orderedappend_paths ( PlannerInfo root,
RelOptInfo rel,
List live_childrels,
List all_child_pathkeys,
List partitioned_rels 
)
static

Definition at line 1718 of file allpaths.c.

References accumulate_append_subpath(), add_path(), Assert, BackwardScanDirection, bms_make_singleton(), RelOptInfo::boundinfo, build_partition_pathkeys(), RelOptInfo::cheapest_total_path, create_append_path(), create_merge_append_path(), ForwardScanDirection, get_cheapest_path_for_pathkeys(), get_singleton_append_subpath(), IS_SIMPLE_REL, lappend(), lcons(), lfirst, list_make1, NIL, RelOptInfo::nparts, Path::param_info, RelOptInfo::part_scheme, partitions_are_ordered(), pathkeys_contained_in(), RelOptInfo::pathlist, RelOptInfo::relid, RELOPT_JOINREL, RelOptInfo::reloptkind, RTE_SUBQUERY, RelOptInfo::rtekind, STARTUP_COST, and TOTAL_COST.

Referenced by add_paths_to_append_rel().

1722 {
1723  ListCell *lcp;
1724  List *partition_pathkeys = NIL;
1725  List *partition_pathkeys_desc = NIL;
1726  bool partition_pathkeys_partial = true;
1727  bool partition_pathkeys_desc_partial = true;
1728  List *startup_partitioned_rels = NIL;
1729  List *total_partitioned_rels = NIL;
1730  bool flatten_partitioned_rels;
1731 
1732  /* Set up the method for building the partitioned rels lists */
1733  flatten_partitioned_rels = (rel->rtekind != RTE_SUBQUERY);
1734 
1735  if (rel->reloptkind != RELOPT_JOINREL && rel->part_scheme != NULL)
1736  {
1737  startup_partitioned_rels = list_make1(bms_make_singleton(rel->relid));
1738  total_partitioned_rels = list_make1(bms_make_singleton(rel->relid));
1739  }
1740 
1741  /*
1742  * Some partitioned table setups may allow us to use an Append node
1743  * instead of a MergeAppend. This is possible in cases such as RANGE
1744  * partitioned tables where it's guaranteed that an earlier partition must
1745  * contain rows which come earlier in the sort order. To detect whether
1746  * this is relevant, build pathkey descriptions of the partition ordering,
1747  * for both forward and reverse scans.
1748  */
1749  if (rel->part_scheme != NULL && IS_SIMPLE_REL(rel) &&
1751  {
1752  partition_pathkeys = build_partition_pathkeys(root, rel,
1754  &partition_pathkeys_partial);
1755 
1756  partition_pathkeys_desc = build_partition_pathkeys(root, rel,
1758  &partition_pathkeys_desc_partial);
1759 
1760  /*
1761  * You might think we should truncate_useless_pathkeys here, but
1762  * allowing partition keys which are a subset of the query's pathkeys
1763  * can often be useful. For example, consider a table partitioned by
1764  * RANGE (a, b), and a query with ORDER BY a, b, c. If we have child
1765  * paths that can produce the a, b, c ordering (perhaps via indexes on
1766  * (a, b, c)) then it works to consider the appendrel output as
1767  * ordered by a, b, c.
1768  */
1769  }
1770 
1771  /* Now consider each interesting sort ordering */
1772  foreach(lcp, all_child_pathkeys)
1773  {
1774  List *pathkeys = (List *) lfirst(lcp);
1775  List *startup_subpaths = NIL;
1776  List *total_subpaths = NIL;
1777  bool startup_neq_total = false;
1778  ListCell *lcr;
1779  bool match_partition_order;
1780  bool match_partition_order_desc;
1781 
1782  /*
1783  * Determine if this sort ordering matches any partition pathkeys we
1784  * have, for both ascending and descending partition order. If the
1785  * partition pathkeys happen to be contained in pathkeys then it still
1786  * works, as described above, providing that the partition pathkeys
1787  * are complete and not just a prefix of the partition keys. (In such
1788  * cases we'll be relying on the child paths to have sorted the
1789  * lower-order columns of the required pathkeys.)
1790  */
1791  match_partition_order =
1792  pathkeys_contained_in(pathkeys, partition_pathkeys) ||
1793  (!partition_pathkeys_partial &&
1794  pathkeys_contained_in(partition_pathkeys, pathkeys));
1795 
1796  match_partition_order_desc = !match_partition_order &&
1797  (pathkeys_contained_in(pathkeys, partition_pathkeys_desc) ||
1798  (!partition_pathkeys_desc_partial &&
1799  pathkeys_contained_in(partition_pathkeys_desc, pathkeys)));
1800 
1801  /* Select the child paths for this ordering... */
1802  foreach(lcr, live_childrels)
1803  {
1804  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1805  Path *cheapest_startup,
1806  *cheapest_total;
1807 
1808  /* Locate the right paths, if they are available. */
1809  cheapest_startup =
1811  pathkeys,
1812  NULL,
1813  STARTUP_COST,
1814  false);
1815  cheapest_total =
1817  pathkeys,
1818  NULL,
1819  TOTAL_COST,
1820  false);
1821 
1822  /*
1823  * If we can't find any paths with the right order just use the
1824  * cheapest-total path; we'll have to sort it later.
1825  */
1826  if (cheapest_startup == NULL || cheapest_total == NULL)
1827  {
1828  cheapest_startup = cheapest_total =
1829  childrel->cheapest_total_path;
1830  /* Assert we do have an unparameterized path for this child */
1831  Assert(cheapest_total->param_info == NULL);
1832  }
1833 
1834  /*
1835  * Notice whether we actually have different paths for the
1836  * "cheapest" and "total" cases; frequently there will be no point
1837  * in two create_merge_append_path() calls.
1838  */
1839  if (cheapest_startup != cheapest_total)
1840  startup_neq_total = true;
1841 
1842  /*
1843  * Collect the appropriate child paths. The required logic varies
1844  * for the Append and MergeAppend cases.
1845  */
1846  if (match_partition_order)
1847  {
1848  /*
1849  * We're going to make a plain Append path. We don't need
1850  * most of what accumulate_append_subpath would do, but we do
1851  * want to cut out child Appends or MergeAppends if they have
1852  * just a single subpath (and hence aren't doing anything
1853  * useful).
1854  */
1855  cheapest_startup = get_singleton_append_subpath(cheapest_startup);
1856  cheapest_total = get_singleton_append_subpath(cheapest_total);
1857 
1858  startup_subpaths = lappend(startup_subpaths, cheapest_startup);
1859  total_subpaths = lappend(total_subpaths, cheapest_total);
1860  }
1861  else if (match_partition_order_desc)
1862  {
1863  /*
1864  * As above, but we need to reverse the order of the children,
1865  * because nodeAppend.c doesn't know anything about reverse
1866  * ordering and will scan the children in the order presented.
1867  */
1868  cheapest_startup = get_singleton_append_subpath(cheapest_startup);
1869  cheapest_total = get_singleton_append_subpath(cheapest_total);
1870 
1871  startup_subpaths = lcons(cheapest_startup, startup_subpaths);
1872  total_subpaths = lcons(cheapest_total, total_subpaths);
1873  }
1874  else
1875  {
1876  /*
1877  * Otherwise, rely on accumulate_append_subpath to collect the
1878  * child paths for the MergeAppend.
1879  */
1880  accumulate_append_subpath(cheapest_startup,
1881  &startup_subpaths, NULL,
1882  &startup_partitioned_rels,
1883  flatten_partitioned_rels);
1884  accumulate_append_subpath(cheapest_total,
1885  &total_subpaths, NULL,
1886  &total_partitioned_rels,
1887  flatten_partitioned_rels);
1888  }
1889  }
1890 
1891  /* ... and build the Append or MergeAppend paths */
1892  if (match_partition_order || match_partition_order_desc)
1893  {
1894  /* We only need Append */
1895  add_path(rel, (Path *) create_append_path(root,
1896  rel,
1897  startup_subpaths,
1898  NIL,
1899  pathkeys,
1900  NULL,
1901  0,
1902  false,
1903  startup_partitioned_rels,
1904  -1));
1905  if (startup_neq_total)
1906  add_path(rel, (Path *) create_append_path(root,
1907  rel,
1908  total_subpaths,
1909  NIL,
1910  pathkeys,
1911  NULL,
1912  0,
1913  false,
1914  total_partitioned_rels,
1915  -1));
1916  }
1917  else
1918  {
1919  /* We need MergeAppend */
1920  add_path(rel, (Path *) create_merge_append_path(root,
1921  rel,
1922  startup_subpaths,
1923  pathkeys,
1924  NULL,
1925  startup_partitioned_rels));
1926  if (startup_neq_total)
1927  add_path(rel, (Path *) create_merge_append_path(root,
1928  rel,
1929  total_subpaths,
1930  pathkeys,
1931  NULL,
1932  total_partitioned_rels));
1933  }
1934  }
1935 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:404
#define NIL
Definition: pg_list.h:65
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
RelOptKind reloptkind
Definition: pathnodes.h:663
List * build_partition_pathkeys(PlannerInfo *root, RelOptInfo *partrel, ScanDirection scandir, bool *partialkeys)
Definition: pathkeys.c:699
static void accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths, List **partitioned_rels, bool flatten_partitioned_rels)
Definition: allpaths.c:2094
ParamPathInfo * param_info
Definition: pathnodes.h:1151
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:639
#define list_make1(x1)
Definition: pg_list.h:206
struct Path * cheapest_total_path
Definition: pathnodes.h:684
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
int nparts
Definition: pathnodes.h:744
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer, List *partitioned_rels)
Definition: pathnode.c:1377
static Path * get_singleton_append_subpath(Path *path)
Definition: allpaths.c:2151
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, List *partitioned_rels, double rows)
Definition: pathnode.c:1215
Index relid
Definition: pathnodes.h:694
List * lappend(List *list, void *datum)
Definition: list.c:336
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:324
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:747
RTEKind rtekind
Definition: pathnodes.h:696
List * lcons(void *datum, List *list)
Definition: list.c:468
bool partitions_are_ordered(PartitionBoundInfo boundinfo, int nparts)
Definition: partbounds.c:2748
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
PartitionScheme part_scheme
Definition: pathnodes.h:743
List * pathlist
Definition: pathnodes.h:680
Definition: pg_list.h:50

◆ generate_partitionwise_join_paths()

void generate_partitionwise_join_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 3972 of file allpaths.c.

References add_paths_to_append_rel(), Alias::aliasname, Assert, RelOptInfo::baserestrictinfo, bms_next_member(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, check_stack_depth(), RestrictInfo::clause, RelOptInfo::consider_partitionwise_join, RangeTblEntry::eref, generate_partitionwise_join_paths(), i, JoinPath::innerjoinpath, MergePath::innersortkeys, IS_DUMMY_REL, IS_JOIN_REL, IS_PARTITIONED_REL, IsA, RelOptInfo::joininfo, JoinPath::joinrestrictinfo, lappend(), lfirst, list_free(), lnext(), mark_dummy_rel(), MergePath::materialize_inner, NIL, nodeTag, RelOptInfo::nparts, JoinPath::outerjoinpath, MergePath::outersortkeys, Path::param_info, Path::parent, PlannerInfo::parse, RelOptInfo::part_rels, Path::pathkeys, RelOptInfo::pathlist, Path::pathtype, ParamPathInfo::ppi_req_outer, print_expr(), print_pathkeys(), printf, RelOptInfo::relids, RelOptInfo::reltarget, RelOptInfo::rows, Path::rows, Query::rtable, set_cheapest(), PlannerInfo::simple_rte_array, Path::startup_cost, generate_unaccent_rules::stdout, subpath(), T_AggPath, T_AppendPath, T_BitmapAndPath, T_BitmapHeapPath, T_BitmapOrPath, T_CteScan, T_CustomPath, T_ForeignPath, T_FunctionScan, T_GatherMergePath, T_GatherPath, T_GroupingSetsPath, T_GroupPath, T_GroupResultPath, T_HashPath, T_IncrementalSortPath, T_IndexPath, T_LimitPath, T_LockRowsPath, T_MaterialPath, T_MergeAppendPath, T_MergePath, T_MinMaxAggPath, T_ModifyTablePath, T_NamedTuplestoreScan, T_NestPath, T_Path, T_ProjectionPath, T_ProjectSetPath, T_RecursiveUnionPath, T_Result, T_SampleScan, T_SeqScan, T_SetOpPath, T_SortPath, T_SubqueryScanPath, T_TableFuncScan, T_TidPath, T_UniquePath, T_UpperUniquePath, T_ValuesScan, T_WindowAggPath, T_WorkTableScan, Path::total_cost, and PathTarget::width.

Referenced by generate_partitionwise_join_paths(), merge_clump(), and standard_join_search().

3973 {
3974  List *live_children = NIL;
3975  int cnt_parts;
3976  int num_parts;
3977  RelOptInfo **part_rels;
3978 
3979  /* Handle only join relations here. */
3980  if (!IS_JOIN_REL(rel))
3981  return;
3982 
3983  /* We've nothing to do if the relation is not partitioned. */
3984  if (!IS_PARTITIONED_REL(rel))
3985  return;
3986 
3987  /* The relation should have consider_partitionwise_join set. */
3989 
3990  /* Guard against stack overflow due to overly deep partition hierarchy. */
3992 
3993  num_parts = rel->nparts;
3994  part_rels = rel->part_rels;
3995 
3996  /* Collect non-dummy child-joins. */
3997  for (cnt_parts = 0; cnt_parts < num_parts; cnt_parts++)
3998  {
3999  RelOptInfo *child_rel = part_rels[cnt_parts];
4000 
4001  /* If it's been pruned entirely, it's certainly dummy. */
4002  if (child_rel == NULL)
4003  continue;
4004 
4005  /* Add partitionwise join paths for partitioned child-joins. */
4006  generate_partitionwise_join_paths(root, child_rel);
4007 
4008  set_cheapest(child_rel);
4009 
4010  /* Dummy children will not be scanned, so ignore those. */
4011  if (IS_DUMMY_REL(child_rel))
4012  continue;
4013 
4014 #ifdef OPTIMIZER_DEBUG
4015  debug_print_rel(root, child_rel);
4016 #endif
4017 
4018  live_children = lappend(live_children, child_rel);
4019  }
4020 
4021  /* If all child-joins are dummy, parent join is also dummy. */
4022  if (!live_children)
4023  {
4024  mark_dummy_rel(rel);
4025  return;
4026  }
4027 
4028  /* Build additional paths for this rel from child-join paths. */
4029  add_paths_to_append_rel(root, rel, live_children);
4030  list_free(live_children);
4031 }
#define NIL
Definition: pg_list.h:65
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1289
void generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:3972
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:644
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
void check_stack_depth(void)
Definition: postgres.c:3377
int nparts
Definition: pathnodes.h:744
List * lappend(List *list, void *datum)
Definition: list.c:336
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
bool consider_partitionwise_join
Definition: pathnodes.h:737
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1261
#define Assert(condition)
Definition: c.h:792
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:766
void list_free(List *list)
Definition: list.c:1391
Definition: pg_list.h:50

◆ generate_useful_gather_paths()

void generate_useful_gather_paths ( PlannerInfo root,
RelOptInfo rel,
bool  override_rows 
)

Definition at line 2884 of file allpaths.c.

References add_path(), Assert, create_gather_merge_path(), create_incremental_sort_path(), create_sort_path(), enable_incremental_sort, generate_gather_paths(), get_useful_pathkeys_for_relation(), lfirst, linitial, list_length(), NIL, Path::parallel_workers, RelOptInfo::partial_pathlist, GatherMergePath::path, Path::pathkeys, pathkeys_count_contained_in(), RelOptInfo::reltarget, Path::rows, and subpath().

Referenced by apply_scanjoin_target_to_paths(), gather_grouping_paths(), merge_clump(), set_rel_pathlist(), and standard_join_search().

2885 {
2886  ListCell *lc;
2887  double rows;
2888  double *rowsp = NULL;
2889  List *useful_pathkeys_list = NIL;
2890  Path *cheapest_partial_path = NULL;
2891 
2892  /* If there are no partial paths, there's nothing to do here. */
2893  if (rel->partial_pathlist == NIL)
2894  return;
2895 
2896  /* Should we override the rel's rowcount estimate? */
2897  if (override_rows)
2898  rowsp = &rows;
2899 
2900  /* generate the regular gather (merge) paths */
2901  generate_gather_paths(root, rel, override_rows);
2902 
2903  /* consider incremental sort for interesting orderings */
2904  useful_pathkeys_list = get_useful_pathkeys_for_relation(root, rel, true);
2905 
2906  /* used for explicit (full) sort paths */
2907  cheapest_partial_path = linitial(rel->partial_pathlist);
2908 
2909  /*
2910  * Consider sorted paths for each interesting ordering. We generate both
2911  * incremental and full sort.
2912  */
2913  foreach(lc, useful_pathkeys_list)
2914  {
2915  List *useful_pathkeys = lfirst(lc);
2916  ListCell *lc2;
2917  bool is_sorted;
2918  int presorted_keys;
2919 
2920  foreach(lc2, rel->partial_pathlist)
2921  {
2922  Path *subpath = (Path *) lfirst(lc2);
2923  GatherMergePath *path;
2924 
2925  is_sorted = pathkeys_count_contained_in(useful_pathkeys,
2926  subpath->pathkeys,
2927  &presorted_keys);
2928 
2929  /*
2930  * We don't need to consider the case where a subpath is already
2931  * fully sorted because generate_gather_paths already creates a
2932  * gather merge path for every subpath that has pathkeys present.
2933  *
2934  * But since the subpath is already sorted, we know we don't need
2935  * to consider adding a sort (other either kind) on top of it, so
2936  * we can continue here.
2937  */
2938  if (is_sorted)
2939  continue;
2940 
2941  /*
2942  * Consider regular sort for the cheapest partial path (for each
2943  * useful pathkeys). We know the path is not sorted, because we'd
2944  * not get here otherwise.
2945  *
2946  * This is not redundant with the gather paths created in
2947  * generate_gather_paths, because that doesn't generate ordered
2948  * output. Here we add an explicit sort to match the useful
2949  * ordering.
2950  */
2951  if (cheapest_partial_path == subpath)
2952  {
2953  Path *tmp;
2954 
2955  tmp = (Path *) create_sort_path(root,
2956  rel,
2957  subpath,
2958  useful_pathkeys,
2959  -1.0);
2960 
2961  rows = tmp->rows * tmp->parallel_workers;
2962 
2963  path = create_gather_merge_path(root, rel,
2964  tmp,
2965  rel->reltarget,
2966  tmp->pathkeys,
2967  NULL,
2968  rowsp);
2969 
2970  add_path(rel, &path->path);
2971 
2972  /* Fall through */
2973  }
2974 
2975  /*
2976  * Consider incremental sort, but only when the subpath is already
2977  * partially sorted on a pathkey prefix.
2978  */
2979  if (enable_incremental_sort && presorted_keys > 0)
2980  {
2981  Path *tmp;
2982 
2983  /*
2984  * We should have already excluded pathkeys of length 1
2985  * because then presorted_keys > 0 would imply is_sorted was
2986  * true.
2987  */
2988  Assert(list_length(useful_pathkeys) != 1);
2989 
2990  tmp = (Path *) create_incremental_sort_path(root,
2991  rel,
2992  subpath,
2993  useful_pathkeys,
2994  presorted_keys,
2995  -1);
2996 
2997  path = create_gather_merge_path(root, rel,
2998  tmp,
2999  rel->reltarget,
3000  tmp->pathkeys,
3001  NULL,
3002  rowsp);
3003 
3004  add_path(rel, &path->path);
3005  }
3006  }
3007  }
3008 }
#define NIL
Definition: pg_list.h:65
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
bool enable_incremental_sort
Definition: costsize.c:138
int parallel_workers
Definition: pathnodes.h:1155
List * partial_pathlist
Definition: pathnodes.h:682
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:2745
IncrementalSortPath * create_incremental_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, int presorted_keys, double limit_tuples)
Definition: pathnode.c:2802
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition: pathkeys.c:343
#define linitial(l)
Definition: pg_list.h:174
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1787
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
Definition: pathnode.c:2851
List * pathkeys
Definition: pathnodes.h:1162
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
double rows
Definition: pathnodes.h:1158
static int list_length(const List *l)
Definition: pg_list.h:149
static List * get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel, bool require_parallel_safe)
Definition: allpaths.c:2815
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:677
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241

◆ get_cheapest_parameterized_child_path()

static Path * get_cheapest_parameterized_child_path ( PlannerInfo root,
RelOptInfo rel,
Relids  required_outer 
)
static

Definition at line 1945 of file allpaths.c.

References Assert, bms_equal(), bms_is_subset(), compare_path_costs(), get_cheapest_path_for_pathkeys(), lfirst, NIL, PATH_REQ_OUTER, RelOptInfo::pathlist, reparameterize_path(), and TOTAL_COST.

Referenced by add_paths_to_append_rel().

1947 {
1948  Path *cheapest;
1949  ListCell *lc;
1950 
1951  /*
1952  * Look up the cheapest existing path with no more than the needed
1953  * parameterization. If it has exactly the needed parameterization, we're
1954  * done.
1955  */
1956  cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
1957  NIL,
1958  required_outer,
1959  TOTAL_COST,
1960  false);
1961  Assert(cheapest != NULL);
1962  if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
1963  return cheapest;
1964 
1965  /*
1966  * Otherwise, we can "reparameterize" an existing path to match the given
1967  * parameterization, which effectively means pushing down additional
1968  * joinquals to be checked within the path's scan. However, some existing
1969  * paths might check the available joinquals already while others don't;
1970  * therefore, it's not clear which existing path will be cheapest after
1971  * reparameterization. We have to go through them all and find out.
1972  */
1973  cheapest = NULL;
1974  foreach(lc, rel->pathlist)
1975  {
1976  Path *path = (Path *) lfirst(lc);
1977 
1978  /* Can't use it if it needs more than requested parameterization */
1979  if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
1980  continue;
1981 
1982  /*
1983  * Reparameterization can only increase the path's cost, so if it's
1984  * already more expensive than the current cheapest, forget it.
1985  */
1986  if (cheapest != NULL &&
1987  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1988  continue;
1989 
1990  /* Reparameterize if needed, then recheck cost */
1991  if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
1992  {
1993  path = reparameterize_path(root, path, required_outer, 1.0);
1994  if (path == NULL)
1995  continue; /* failed to reparameterize this one */
1996  Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
1997 
1998  if (cheapest != NULL &&
1999  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2000  continue;
2001  }
2002 
2003  /* We have a new best path */
2004  cheapest = path;
2005  }
2006 
2007  /* Return the best path, or NULL if we found no suitable candidate */
2008  return cheapest;
2009 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:404
#define NIL
Definition: pg_list.h:65
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:71
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1167
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
Path * reparameterize_path(PlannerInfo *root, Path *path, Relids required_outer, double loop_count)
Definition: pathnode.c:3761
List * pathlist
Definition: pathnodes.h:680
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ get_singleton_append_subpath()

static Path * get_singleton_append_subpath ( Path path)
static

Definition at line 2151 of file allpaths.c.

References Assert, IsA, linitial, list_length(), Path::parallel_aware, AppendPath::subpaths, and MergeAppendPath::subpaths.

Referenced by generate_orderedappend_paths().

2152 {
2153  Assert(!path->parallel_aware);
2154 
2155  if (IsA(path, AppendPath))
2156  {
2157  AppendPath *apath = (AppendPath *) path;
2158 
2159  if (list_length(apath->subpaths) == 1)
2160  return (Path *) linitial(apath->subpaths);
2161  }
2162  else if (IsA(path, MergeAppendPath))
2163  {
2164  MergeAppendPath *mpath = (MergeAppendPath *) path;
2165 
2166  if (list_length(mpath->subpaths) == 1)
2167  return (Path *) linitial(mpath->subpaths);
2168  }
2169 
2170  return path;
2171 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
List * subpaths
Definition: pathnodes.h:1409
#define linitial(l)
Definition: pg_list.h:174
#define Assert(condition)
Definition: c.h:792
static int list_length(const List *l)
Definition: pg_list.h:149
bool parallel_aware
Definition: pathnodes.h:1153

◆ get_useful_pathkeys_for_relation()

static List* get_useful_pathkeys_for_relation ( PlannerInfo root,
RelOptInfo rel,
bool  require_parallel_safe 
)
static

Definition at line 2815 of file allpaths.c.

References find_em_expr_usable_for_sorting_rel(), lappend(), lfirst, list_copy(), list_length(), list_truncate(), NIL, PathKey::pk_eclass, and PlannerInfo::query_pathkeys.

Referenced by generate_useful_gather_paths().

2817 {
2818  List *useful_pathkeys_list = NIL;
2819 
2820  /*
2821  * Considering query_pathkeys is always worth it, because it might allow
2822  * us to avoid a total sort when we have a partially presorted path
2823  * available or to push the total sort into the parallel portion of the
2824  * query.
2825  */
2826  if (root->query_pathkeys)
2827  {
2828  ListCell *lc;
2829  int npathkeys = 0; /* useful pathkeys */
2830 
2831  foreach(lc, root->query_pathkeys)
2832  {
2833  PathKey *pathkey = (PathKey *) lfirst(lc);
2834  EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
2835 
2836  /*
2837  * We can only build a sort for pathkeys which contain an EC
2838  * member in the current relation's target, so ignore any suffix
2839  * of the list as soon as we find a pathkey without an EC member
2840  * in the relation.
2841  *
2842  * By still returning the prefix of the pathkeys list that does
2843  * meet criteria of EC membership in the current relation, we
2844  * enable not just an incremental sort on the entirety of
2845  * query_pathkeys but also incremental sort below a JOIN.
2846  *
2847  * If requested, ensure the expression is parallel safe too.
2848  */
2849  if (!find_em_expr_usable_for_sorting_rel(root, pathkey_ec, rel,
2850  require_parallel_safe))
2851  break;
2852 
2853  npathkeys++;
2854  }
2855 
2856  /*
2857  * The whole query_pathkeys list matches, so append it directly, to
2858  * allow comparing pathkeys easily by comparing list pointer. If we
2859  * have to truncate the pathkeys, we gotta do a copy though.
2860  */
2861  if (npathkeys == list_length(root->query_pathkeys))
2862  useful_pathkeys_list = lappend(useful_pathkeys_list,
2863  root->query_pathkeys);
2864  else if (npathkeys > 0)
2865  useful_pathkeys_list = lappend(useful_pathkeys_list,
2867  npathkeys));
2868  }
2869 
2870  return useful_pathkeys_list;
2871 }
#define NIL
Definition: pg_list.h:65
List * query_pathkeys
Definition: pathnodes.h:292
List * list_truncate(List *list, int new_size)
Definition: list.c:600
List * list_copy(const List *oldlist)
Definition: list.c:1418
Expr * find_em_expr_usable_for_sorting_rel(PlannerInfo *root, EquivalenceClass *ec, RelOptInfo *rel, bool require_parallel_safe)
Definition: equivclass.c:808
List * lappend(List *list, void *datum)
Definition: list.c:336
#define lfirst(lc)
Definition: pg_list.h:169
EquivalenceClass * pk_eclass
Definition: pathnodes.h:1045
static int list_length(const List *l)
Definition: pg_list.h:149
Definition: pg_list.h:50

◆ has_multiple_baserels()

static bool has_multiple_baserels ( PlannerInfo root)
static

Definition at line 2211 of file allpaths.c.

References RELOPT_BASEREL, RelOptInfo::reloptkind, PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by set_subquery_pathlist().

2212 {
2213  int num_base_rels = 0;
2214  Index rti;
2215 
2216  for (rti = 1; rti < root->simple_rel_array_size; rti++)
2217  {
2218  RelOptInfo *brel = root->simple_rel_array[rti];
2219 
2220  if (brel == NULL)
2221  continue;
2222 
2223  /* ignore RTEs that are "other rels" */
2224  if (brel->reloptkind == RELOPT_BASEREL)
2225  if (++num_base_rels > 1)
2226  return true;
2227  }
2228  return false;
2229 }
RelOptKind reloptkind
Definition: pathnodes.h:663
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
int simple_rel_array_size
Definition: pathnodes.h:198
unsigned int Index
Definition: c.h:537

◆ make_one_rel()

RelOptInfo* make_one_rel ( PlannerInfo root,
List joinlist 
)

Definition at line 157 of file allpaths.c.

References PlannerInfo::all_baserels, Assert, bms_add_member(), bms_equal(), IS_DUMMY_REL, IS_SIMPLE_REL, make_rel_from_joinlist(), RelOptInfo::pages, RelOptInfo::relid, RelOptInfo::relids, RELOPT_BASEREL, RelOptInfo::reloptkind, set_base_rel_consider_startup(), set_base_rel_pathlists(), set_base_rel_sizes(), PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and PlannerInfo::total_table_pages.

Referenced by query_planner().

158 {
159  RelOptInfo *rel;
160  Index rti;
161  double total_pages;
162 
163  /*
164  * Construct the all_baserels Relids set.
165  */
166  root->all_baserels = NULL;
167  for (rti = 1; rti < root->simple_rel_array_size; rti++)
168  {
169  RelOptInfo *brel = root->simple_rel_array[rti];
170 
171  /* there may be empty slots corresponding to non-baserel RTEs */
172  if (brel == NULL)
173  continue;
174 
175  Assert(brel->relid == rti); /* sanity check on array */
176 
177  /* ignore RTEs that are "other rels" */
178  if (brel->reloptkind != RELOPT_BASEREL)
179  continue;
180 
181  root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
182  }
183 
184  /* Mark base rels as to whether we care about fast-start plans */
186 
187  /*
188  * Compute size estimates and consider_parallel flags for each base rel.
189  */
190  set_base_rel_sizes(root);
191 
192  /*
193  * We should now have size estimates for every actual table involved in
194  * the query, and we also know which if any have been deleted from the
195  * query by join removal, pruned by partition pruning, or eliminated by
196  * constraint exclusion. So we can now compute total_table_pages.
197  *
198  * Note that appendrels are not double-counted here, even though we don't
199  * bother to distinguish RelOptInfos for appendrel parents, because the
200  * parents will have pages = 0.
201  *
202  * XXX if a table is self-joined, we will count it once per appearance,
203  * which perhaps is the wrong thing ... but that's not completely clear,
204  * and detecting self-joins here is difficult, so ignore it for now.
205  */
206  total_pages = 0;
207  for (rti = 1; rti < root->simple_rel_array_size; rti++)
208  {
209  RelOptInfo *brel = root->simple_rel_array[rti];
210 
211  if (brel == NULL)
212  continue;
213 
214  Assert(brel->relid == rti); /* sanity check on array */
215 
216  if (IS_DUMMY_REL(brel))
217  continue;
218 
219  if (IS_SIMPLE_REL(brel))
220  total_pages += (double) brel->pages;
221  }
222  root->total_table_pages = total_pages;
223 
224  /*
225  * Generate access paths for each base rel.
226  */
228 
229  /*
230  * Generate access paths for the entire join tree.
231  */
232  rel = make_rel_from_joinlist(root, joinlist);
233 
234  /*
235  * The result should join all and only the query's base rels.
236  */
237  Assert(bms_equal(rel->relids, root->all_baserels));
238 
239  return rel;
240 }
RelOptKind reloptkind
Definition: pathnodes.h:663
static void set_base_rel_sizes(PlannerInfo *root)
Definition: allpaths.c:296
static void set_base_rel_consider_startup(PlannerInfo *root)
Definition: allpaths.c:253
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:639
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
Relids all_baserels
Definition: pathnodes.h:221
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:3018
Relids relids
Definition: pathnodes.h:666
double total_table_pages
Definition: pathnodes.h:327
int simple_rel_array_size
Definition: pathnodes.h:198
Index relid
Definition: pathnodes.h:694
static void set_base_rel_pathlists(PlannerInfo *root)
Definition: allpaths.c:339
unsigned int Index
Definition: c.h:537
BlockNumber pages
Definition: pathnodes.h:705
#define Assert(condition)
Definition: c.h:792
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ make_rel_from_joinlist()

static RelOptInfo * make_rel_from_joinlist ( PlannerInfo root,
List joinlist 
)
static

Definition at line 3018 of file allpaths.c.

References elog, enable_geqo, ERROR, find_base_rel(), geqo(), geqo_threshold, PlannerInfo::initial_rels, IsA, join_search_hook, lappend(), lfirst, linitial, list_length(), NIL, nodeTag, and standard_join_search().

Referenced by make_one_rel().

3019 {
3020  int levels_needed;
3021  List *initial_rels;
3022  ListCell *jl;
3023 
3024  /*
3025  * Count the number of child joinlist nodes. This is the depth of the
3026  * dynamic-programming algorithm we must employ to consider all ways of
3027  * joining the child nodes.
3028  */
3029  levels_needed = list_length(joinlist);
3030 
3031  if (levels_needed <= 0)
3032  return NULL; /* nothing to do? */
3033 
3034  /*
3035  * Construct a list of rels corresponding to the child joinlist nodes.
3036  * This may contain both base rels and rels constructed according to
3037  * sub-joinlists.
3038  */
3039  initial_rels = NIL;
3040  foreach(jl, joinlist)
3041  {
3042  Node *jlnode = (Node *) lfirst(jl);
3043  RelOptInfo *thisrel;
3044 
3045  if (IsA(jlnode, RangeTblRef))
3046  {
3047  int varno = ((RangeTblRef *) jlnode)->rtindex;
3048 
3049  thisrel = find_base_rel(root, varno);
3050  }
3051  else if (IsA(jlnode, List))
3052  {
3053  /* Recurse to handle subproblem */
3054  thisrel = make_rel_from_joinlist(root, (List *) jlnode);
3055  }
3056  else
3057  {
3058  elog(ERROR, "unrecognized joinlist node type: %d",
3059  (int) nodeTag(jlnode));
3060  thisrel = NULL; /* keep compiler quiet */
3061  }
3062 
3063  initial_rels = lappend(initial_rels, thisrel);
3064  }
3065 
3066  if (levels_needed == 1)
3067  {
3068  /*
3069  * Single joinlist node, so we're done.
3070  */
3071  return (RelOptInfo *) linitial(initial_rels);
3072  }
3073  else
3074  {
3075  /*
3076  * Consider the different orders in which we could join the rels,
3077  * using a plugin, GEQO, or the regular join search code.
3078  *
3079  * We put the initial_rels list into a PlannerInfo field because
3080  * has_legal_joinclause() needs to look at it (ugly :-().
3081  */
3082  root->initial_rels = initial_rels;
3083 
3084  if (join_search_hook)
3085  return (*join_search_hook) (root, levels_needed, initial_rels);
3086  else if (enable_geqo && levels_needed >= geqo_threshold)
3087  return geqo(root, levels_needed, initial_rels);
3088  else
3089  return standard_join_search(root, levels_needed, initial_rels);
3090  }
3091 }
RelOptInfo * standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
Definition: allpaths.c:3123
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Definition: nodes.h:528
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
bool enable_geqo
Definition: allpaths.c:62
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:3018
List * lappend(List *list, void *datum)
Definition: list.c:336
join_search_hook_type join_search_hook
Definition: allpaths.c:71
int geqo_threshold
Definition: allpaths.c:63
RelOptInfo * geqo(PlannerInfo *root, int number_of_rels, List *initial_rels)
Definition: geqo_main.c:67
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define nodeTag(nodeptr)
Definition: nodes.h:533
#define elog(elevel,...)
Definition: elog.h:228
List * initial_rels
Definition: pathnodes.h:302
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
Definition: pg_list.h:50

◆ qual_is_pushdown_safe()

static bool qual_is_pushdown_safe ( Query subquery,
Index  rti,
Node qual,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3558 of file allpaths.c.

References Assert, contain_leaked_vars(), contain_subplans(), contain_volatile_functions(), contain_window_function(), IsA, lfirst, list_free(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, pushdown_safety_info::unsafeColumns, pushdown_safety_info::unsafeLeaky, pushdown_safety_info::unsafeVolatile, Var::varattno, and Var::varno.

Referenced by set_subquery_pathlist().

3560 {
3561  bool safe = true;
3562  List *vars;
3563  ListCell *vl;
3564 
3565  /* Refuse subselects (point 1) */
3566  if (contain_subplans(qual))
3567  return false;
3568 
3569  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
3570  if (safetyInfo->unsafeVolatile &&
3572  return false;
3573 
3574  /* Refuse leaky quals if told to (point 3) */
3575  if (safetyInfo->unsafeLeaky &&
3576  contain_leaked_vars(qual))
3577  return false;
3578 
3579  /*
3580  * It would be unsafe to push down window function calls, but at least for
3581  * the moment we could never see any in a qual anyhow. (The same applies
3582  * to aggregates, which we check for in pull_var_clause below.)
3583  */
3585 
3586  /*
3587  * Examine all Vars used in clause. Since it's a restriction clause, all
3588  * such Vars must refer to subselect output columns ... unless this is
3589  * part of a LATERAL subquery, in which case there could be lateral
3590  * references.
3591  */
3593  foreach(vl, vars)
3594  {
3595  Var *var = (Var *) lfirst(vl);
3596 
3597  /*
3598  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
3599  * It's not clear whether a PHV could safely be pushed down, and even
3600  * less clear whether such a situation could arise in any cases of
3601  * practical interest anyway. So for the moment, just refuse to push
3602  * down.
3603  */
3604  if (!IsA(var, Var))
3605  {
3606  safe = false;
3607  break;
3608  }
3609 
3610  /*
3611  * Punt if we find any lateral references. It would be safe to push
3612  * these down, but we'd have to convert them into outer references,
3613  * which subquery_push_qual lacks the infrastructure to do. The case
3614  * arises so seldom that it doesn't seem worth working hard on.
3615  */
3616  if (var->varno != rti)
3617  {
3618  safe = false;
3619  break;
3620  }
3621 
3622  /* Subqueries have no system columns */
3623  Assert(var->varattno >= 0);
3624 
3625  /* Check point 4 */
3626  if (var->varattno == 0)
3627  {
3628  safe = false;
3629  break;
3630  }
3631 
3632  /* Check point 5 */
3633  if (safetyInfo->unsafeColumns[var->varattno])
3634  {
3635  safe = false;
3636  break;
3637  }
3638  }
3639 
3640  list_free(vars);
3641 
3642  return safe;
3643 }
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1077
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
AttrNumber varattno
Definition: primnodes.h:186
bool * unsafeColumns
Definition: allpaths.c:56
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:437
Definition: primnodes.h:181
bool contain_subplans(Node *clause)
Definition: clauses.c:323
bool contain_window_function(Node *clause)
Definition: clauses.c:207
Index varno
Definition: primnodes.h:184
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:186
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
void list_free(List *list)
Definition: list.c:1391
Definition: regcomp.c:224
Definition: pg_list.h:50

◆ recurse_push_qual()

static void recurse_push_qual ( Node setOp,
Query topquery,
RangeTblEntry rte,
Index  rti,
Node qual 
)
static

Definition at line 3696 of file allpaths.c.

References Assert, elog, ERROR, IsA, SetOperationStmt::larg, nodeTag, SetOperationStmt::rarg, rt_fetch, Query::rtable, RangeTblRef::rtindex, RangeTblEntry::subquery, and subquery_push_qual().

Referenced by subquery_push_qual().

3698 {
3699  if (IsA(setOp, RangeTblRef))
3700  {
3701  RangeTblRef *rtr = (RangeTblRef *) setOp;
3702  RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
3703  Query *subquery = subrte->subquery;
3704 
3705  Assert(subquery != NULL);
3706  subquery_push_qual(subquery, rte, rti, qual);
3707  }
3708  else if (IsA(setOp, SetOperationStmt))
3709  {
3710  SetOperationStmt *op = (SetOperationStmt *) setOp;
3711 
3712  recurse_push_qual(op->larg, topquery, rte, rti, qual);
3713  recurse_push_qual(op->rarg, topquery, rte, rti, qual);
3714  }
3715  else
3716  {
3717  elog(ERROR, "unrecognized node type: %d",
3718  (int) nodeTag(setOp));
3719  }
3720 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:45
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:792
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3649
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3696
#define nodeTag(nodeptr)
Definition: nodes.h:533
Query * subquery
Definition: parsenodes.h:1016
#define elog(elevel,...)
Definition: elog.h:228

◆ recurse_pushdown_safe()

static bool recurse_pushdown_safe ( Node setOp,
Query topquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3348 of file allpaths.c.

References Assert, elog, ERROR, IsA, SetOperationStmt::larg, nodeTag, SetOperationStmt::op, SetOperationStmt::rarg, rt_fetch, Query::rtable, RangeTblRef::rtindex, SETOP_EXCEPT, RangeTblEntry::subquery, and subquery_is_pushdown_safe().

Referenced by subquery_is_pushdown_safe().

3350 {
3351  if (IsA(setOp, RangeTblRef))
3352  {
3353  RangeTblRef *rtr = (RangeTblRef *) setOp;
3354  RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
3355  Query *subquery = rte->subquery;
3356 
3357  Assert(subquery != NULL);
3358  return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
3359  }
3360  else if (IsA(setOp, SetOperationStmt))
3361  {
3362  SetOperationStmt *op = (SetOperationStmt *) setOp;
3363 
3364  /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
3365  if (op->op == SETOP_EXCEPT)
3366  return false;
3367  /* Else recurse */
3368  if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
3369  return false;
3370  if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
3371  return false;
3372  }
3373  else
3374  {
3375  elog(ERROR, "unrecognized node type: %d",
3376  (int) nodeTag(setOp));
3377  }
3378  return true;
3379 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3348
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:45
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:792
SetOperation op
Definition: parsenodes.h:1663
#define nodeTag(nodeptr)
Definition: nodes.h:533
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3292
Query * subquery
Definition: parsenodes.h:1016
#define elog(elevel,...)
Definition: elog.h:228

◆ remove_unused_subquery_outputs()

static void remove_unused_subquery_outputs ( Query subquery,
RelOptInfo rel 
)
static

Definition at line 3743 of file allpaths.c.

References RelOptInfo::baserestrictinfo, bms_is_member(), RestrictInfo::clause, contain_volatile_functions(), Query::distinctClause, TargetEntry::expr, exprCollation(), expression_returns_set(), PathTarget::exprs, exprType(), exprTypmod(), FirstLowInvalidHeapAttributeNumber, Query::hasDistinctOn, Query::hasTargetSRFs, lfirst, makeNullConst(), pull_varattnos(), RelOptInfo::relid, RelOptInfo::reltarget, TargetEntry::resjunk, TargetEntry::resno, TargetEntry::ressortgroupref, Query::setOperations, and Query::targetList.

Referenced by set_subquery_pathlist().

3744 {
3745  Bitmapset *attrs_used = NULL;
3746  ListCell *lc;
3747 
3748  /*
3749  * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
3750  * could update all the child SELECTs' tlists, but it seems not worth the
3751  * trouble presently.
3752  */
3753  if (subquery->setOperations)
3754  return;
3755 
3756  /*
3757  * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
3758  * time: all its output columns must be used in the distinctClause.
3759  */
3760  if (subquery->distinctClause && !subquery->hasDistinctOn)
3761  return;
3762 
3763  /*
3764  * Collect a bitmap of all the output column numbers used by the upper
3765  * query.
3766  *
3767  * Add all the attributes needed for joins or final output. Note: we must
3768  * look at rel's targetlist, not the attr_needed data, because attr_needed
3769  * isn't computed for inheritance child rels, cf set_append_rel_size().
3770  * (XXX might be worth changing that sometime.)
3771  */
3772  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
3773 
3774  /* Add all the attributes used by un-pushed-down restriction clauses. */
3775  foreach(lc, rel->baserestrictinfo)
3776  {
3777  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3778 
3779  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
3780  }
3781 
3782  /*
3783  * If there's a whole-row reference to the subquery, we can't remove
3784  * anything.
3785  */
3787  return;
3788 
3789  /*
3790  * Run through the tlist and zap entries we don't need. It's okay to
3791  * modify the tlist items in-place because set_subquery_pathlist made a
3792  * copy of the subquery.
3793  */
3794  foreach(lc, subquery->targetList)
3795  {
3796  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3797  Node *texpr = (Node *) tle->expr;
3798 
3799  /*
3800  * If it has a sortgroupref number, it's used in some sort/group
3801  * clause so we'd better not remove it. Also, don't remove any
3802  * resjunk columns, since their reason for being has nothing to do
3803  * with anybody reading the subquery's output. (It's likely that
3804  * resjunk columns in a sub-SELECT would always have ressortgroupref
3805  * set, but even if they don't, it seems imprudent to remove them.)
3806  */
3807  if (tle->ressortgroupref || tle->resjunk)
3808  continue;
3809 
3810  /*
3811  * If it's used by the upper query, we can't remove it.
3812  */
3814  attrs_used))
3815  continue;
3816 
3817  /*
3818  * If it contains a set-returning function, we can't remove it since
3819  * that could change the number of rows returned by the subquery.
3820  */
3821  if (subquery->hasTargetSRFs &&
3822  expression_returns_set(texpr))
3823  continue;
3824 
3825  /*
3826  * If it contains volatile functions, we daren't remove it for fear
3827  * that the user is expecting their side-effects to happen.
3828  */
3829  if (contain_volatile_functions(texpr))
3830  continue;
3831 
3832  /*
3833  * OK, we don't need it. Replace the expression with a NULL constant.
3834  * Preserve the exposed type of the expression, in case something
3835  * looks at the rowtype of the subquery's result.
3836  */
3837  tle->expr = (Expr *) makeNullConst(exprType(texpr),
3838  exprTypmod(texpr),
3839  exprCollation(texpr));
3840  }
3841 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
List * baserestrictinfo
Definition: pathnodes.h:728
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:709
Definition: nodes.h:528
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:437
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool hasDistinctOn
Definition: parsenodes.h:129
List * targetList
Definition: parsenodes.h:140
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
bool resjunk
Definition: primnodes.h:1438
List * distinctClause
Definition: parsenodes.h:156
AttrNumber resno
Definition: primnodes.h:1432
Index relid
Definition: pathnodes.h:694
Expr * clause
Definition: pathnodes.h:1994
List * exprs
Definition: pathnodes.h:1078
bool hasTargetSRFs
Definition: parsenodes.h:127
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1431
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Node * setOperations
Definition: parsenodes.h:166
Index ressortgroupref
Definition: primnodes.h:1434
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
struct PathTarget * reltarget
Definition: pathnodes.h:677

◆ set_append_rel_pathlist()

static void set_append_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 1219 of file allpaths.c.

References add_paths_to_append_rel(), PlannerInfo::append_rel_list, AppendRelInfo::child_relid, RelOptInfo::consider_parallel, IS_DUMMY_REL, lappend(), lfirst, NIL, AppendRelInfo::parent_relid, set_rel_pathlist(), PlannerInfo::simple_rel_array, and PlannerInfo::simple_rte_array.

Referenced by set_rel_pathlist().

1221 {
1222  int parentRTindex = rti;
1223  List *live_childrels = NIL;
1224  ListCell *l;
1225 
1226  /*
1227  * Generate access paths for each member relation, and remember the
1228  * non-dummy children.
1229  */
1230  foreach(l, root->append_rel_list)
1231  {
1232  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1233  int childRTindex;
1234  RangeTblEntry *childRTE;
1235  RelOptInfo *childrel;
1236 
1237  /* append_rel_list contains all append rels; ignore others */
1238  if (appinfo->parent_relid != parentRTindex)
1239  continue;
1240 
1241  /* Re-locate the child RTE and RelOptInfo */
1242  childRTindex = appinfo->child_relid;
1243  childRTE = root->simple_rte_array[childRTindex];
1244  childrel = root->simple_rel_array[childRTindex];
1245 
1246  /*
1247  * If set_append_rel_size() decided the parent appendrel was
1248  * parallel-unsafe at some point after visiting this child rel, we
1249  * need to propagate the unsafety marking down to the child, so that
1250  * we don't generate useless partial paths for it.
1251  */
1252  if (!rel->consider_parallel)
1253  childrel->consider_parallel = false;
1254 
1255  /*
1256  * Compute the child's access paths.
1257  */
1258  set_rel_pathlist(root, childrel, childRTindex, childRTE);
1259 
1260  /*
1261  * If child is dummy, ignore it.
1262  */
1263  if (IS_DUMMY_REL(childrel))
1264  continue;
1265 
1266  /*
1267  * Child is live, so add it to the live_childrels list for use below.
1268  */
1269  live_childrels = lappend(live_childrels, childrel);
1270  }
1271 
1272  /* Add paths to the append relation. */
1273  add_paths_to_append_rel(root, rel, live_childrels);
1274 }
#define NIL
Definition: pg_list.h:65
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1289
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
List * lappend(List *list, void *datum)
Definition: list.c:336
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
List * append_rel_list
Definition: pathnodes.h:284
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:475
#define lfirst(lc)
Definition: pg_list.h:169
bool consider_parallel
Definition: pathnodes.h:674
Index child_relid
Definition: pathnodes.h:2239
Index parent_relid
Definition: pathnodes.h:2238
Definition: pg_list.h:50

◆ set_append_rel_size()

static void set_append_rel_size ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 951 of file allpaths.c.

References add_child_rel_equivalences(), adjust_appendrel_attrs(), PlannerInfo::append_rel_list, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, check_stack_depth(), AppendRelInfo::child_relid, RelOptInfo::consider_parallel, RelOptInfo::consider_partitionwise_join, enable_partitionwise_join, PathTarget::exprs, exprType(), exprTypmod(), find_base_rel(), forboth, get_typavgwidth(), PlannerInfo::glob, RelOptInfo::has_eclass_joins, has_useful_pathkeys(), i, InvalidAttrNumber, IS_DUMMY_REL, IS_SIMPLE_REL, IsA, RelOptInfo::joininfo, lfirst, RelOptInfo::max_attr, RelOptInfo::min_attr, palloc0(), PlannerGlobal::parallelModeOK, AppendRelInfo::parent_relid, pfree(), relation_excluded_by_constraints(), RelOptInfo::relid, RangeTblEntry::relkind, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::rows, set_dummy_rel_pathlist(), set_rel_consider_parallel(), set_rel_size(), PlannerInfo::simple_rte_array, RelOptInfo::tuples, Var::varattno, and PathTarget::width.

Referenced by set_rel_size().

953 {
954  int parentRTindex = rti;
955  bool has_live_children;
956  double parent_rows;
957  double parent_size;
958  double *parent_attrsizes;
959  int nattrs;
960  ListCell *l;
961 
962  /* Guard against stack overflow due to overly deep inheritance tree. */
964 
965  Assert(IS_SIMPLE_REL(rel));
966 
967  /*
968  * If this is a partitioned baserel, set the consider_partitionwise_join
969  * flag; currently, we only consider partitionwise joins with the baserel
970  * if its targetlist doesn't contain a whole-row Var.
971  */
973  rel->reloptkind == RELOPT_BASEREL &&
974  rte->relkind == RELKIND_PARTITIONED_TABLE &&
975  rel->attr_needed[InvalidAttrNumber - rel->min_attr] == NULL)
976  rel->consider_partitionwise_join = true;
977 
978  /*
979  * Initialize to compute size estimates for whole append relation.
980  *
981  * We handle width estimates by weighting the widths of different child
982  * rels proportionally to their number of rows. This is sensible because
983  * the use of width estimates is mainly to compute the total relation
984  * "footprint" if we have to sort or hash it. To do this, we sum the
985  * total equivalent size (in "double" arithmetic) and then divide by the
986  * total rowcount estimate. This is done separately for the total rel
987  * width and each attribute.
988  *
989  * Note: if you consider changing this logic, beware that child rels could
990  * have zero rows and/or width, if they were excluded by constraints.
991  */
992  has_live_children = false;
993  parent_rows = 0;
994  parent_size = 0;
995  nattrs = rel->max_attr - rel->min_attr + 1;
996  parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
997 
998  foreach(l, root->append_rel_list)
999  {
1000  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1001  int childRTindex;
1002  RangeTblEntry *childRTE;
1003  RelOptInfo *childrel;
1004  ListCell *parentvars;
1005  ListCell *childvars;
1006 
1007  /* append_rel_list contains all append rels; ignore others */
1008  if (appinfo->parent_relid != parentRTindex)
1009  continue;
1010 
1011  childRTindex = appinfo->child_relid;
1012  childRTE = root->simple_rte_array[childRTindex];
1013 
1014  /*
1015  * The child rel's RelOptInfo was already created during
1016  * add_other_rels_to_query.
1017  */
1018  childrel = find_base_rel(root, childRTindex);
1020 
1021  /* We may have already proven the child to be dummy. */
1022  if (IS_DUMMY_REL(childrel))
1023  continue;
1024 
1025  /*
1026  * We have to copy the parent's targetlist and quals to the child,
1027  * with appropriate substitution of variables. However, the
1028  * baserestrictinfo quals were already copied/substituted when the
1029  * child RelOptInfo was built. So we don't need any additional setup
1030  * before applying constraint exclusion.
1031  */
1032  if (relation_excluded_by_constraints(root, childrel, childRTE))
1033  {
1034  /*
1035  * This child need not be scanned, so we can omit it from the
1036  * appendrel.
1037  */
1038  set_dummy_rel_pathlist(childrel);
1039  continue;
1040  }
1041 
1042  /*
1043  * Constraint exclusion failed, so copy the parent's join quals and
1044  * targetlist to the child, with appropriate variable substitutions.
1045  *
1046  * NB: the resulting childrel->reltarget->exprs may contain arbitrary
1047  * expressions, which otherwise would not occur in a rel's targetlist.
1048  * Code that might be looking at an appendrel child must cope with
1049  * such. (Normally, a rel's targetlist would only include Vars and
1050  * PlaceHolderVars.) XXX we do not bother to update the cost or width
1051  * fields of childrel->reltarget; not clear if that would be useful.
1052  */
1053  childrel->joininfo = (List *)
1055  (Node *) rel->joininfo,
1056  1, &appinfo);
1057  childrel->reltarget->exprs = (List *)
1059  (Node *) rel->reltarget->exprs,
1060  1, &appinfo);
1061 
1062  /*
1063  * We have to make child entries in the EquivalenceClass data
1064  * structures as well. This is needed either if the parent
1065  * participates in some eclass joins (because we will want to consider
1066  * inner-indexscan joins on the individual children) or if the parent
1067  * has useful pathkeys (because we should try to build MergeAppend
1068  * paths that produce those sort orderings).
1069  */
1070  if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1071  add_child_rel_equivalences(root, appinfo, rel, childrel);
1072  childrel->has_eclass_joins = rel->has_eclass_joins;
1073 
1074  /*
1075  * Note: we could compute appropriate attr_needed data for the child's
1076  * variables, by transforming the parent's attr_needed through the
1077  * translated_vars mapping. However, currently there's no need
1078  * because attr_needed is only examined for base relations not
1079  * otherrels. So we just leave the child's attr_needed empty.
1080  */
1081 
1082  /*
1083  * If we consider partitionwise joins with the parent rel, do the same
1084  * for partitioned child rels.
1085  *
1086  * Note: here we abuse the consider_partitionwise_join flag by setting
1087  * it for child rels that are not themselves partitioned. We do so to
1088  * tell try_partitionwise_join() that the child rel is sufficiently
1089  * valid to be used as a per-partition input, even if it later gets
1090  * proven to be dummy. (It's not usable until we've set up the
1091  * reltarget and EC entries, which we just did.)
1092  */
1093  if (rel->consider_partitionwise_join)
1094  childrel->consider_partitionwise_join = true;
1095 
1096  /*
1097  * If parallelism is allowable for this query in general, see whether
1098  * it's allowable for this childrel in particular. But if we've
1099  * already decided the appendrel is not parallel-safe as a whole,
1100  * there's no point in considering parallelism for this child. For
1101  * consistency, do this before calling set_rel_size() for the child.
1102  */
1103  if (root->glob->parallelModeOK && rel->consider_parallel)
1104  set_rel_consider_parallel(root, childrel, childRTE);
1105 
1106  /*
1107  * Compute the child's size.
1108  */
1109  set_rel_size(root, childrel, childRTindex, childRTE);
1110 
1111  /*
1112  * It is possible that constraint exclusion detected a contradiction
1113  * within a child subquery, even though we didn't prove one above. If
1114  * so, we can skip this child.
1115  */
1116  if (IS_DUMMY_REL(childrel))
1117  continue;
1118 
1119  /* We have at least one live child. */
1120  has_live_children = true;
1121 
1122  /*
1123  * If any live child is not parallel-safe, treat the whole appendrel
1124  * as not parallel-safe. In future we might be able to generate plans
1125  * in which some children are farmed out to workers while others are
1126  * not; but we don't have that today, so it's a waste to consider
1127  * partial paths anywhere in the appendrel unless it's all safe.
1128  * (Child rels visited before this one will be unmarked in
1129  * set_append_rel_pathlist().)
1130  */
1131  if (!childrel->consider_parallel)
1132  rel->consider_parallel = false;
1133 
1134  /*
1135  * Accumulate size information from each live child.
1136  */
1137  Assert(childrel->rows > 0);
1138 
1139  parent_rows += childrel->rows;
1140  parent_size += childrel->reltarget->width * childrel->rows;
1141 
1142  /*
1143  * Accumulate per-column estimates too. We need not do anything for
1144  * PlaceHolderVars in the parent list. If child expression isn't a
1145  * Var, or we didn't record a width estimate for it, we have to fall
1146  * back on a datatype-based estimate.
1147  *
1148  * By construction, child's targetlist is 1-to-1 with parent's.
1149  */
1150  forboth(parentvars, rel->reltarget->exprs,
1151  childvars, childrel->reltarget->exprs)
1152  {
1153  Var *parentvar = (Var *) lfirst(parentvars);
1154  Node *childvar = (Node *) lfirst(childvars);
1155 
1156  if (IsA(parentvar, Var))
1157  {
1158  int pndx = parentvar->varattno - rel->min_attr;
1159  int32 child_width = 0;
1160 
1161  if (IsA(childvar, Var) &&
1162  ((Var *) childvar)->varno == childrel->relid)
1163  {
1164  int cndx = ((Var *) childvar)->varattno - childrel->min_attr;
1165 
1166  child_width = childrel->attr_widths[cndx];
1167  }
1168  if (child_width <= 0)
1169  child_width = get_typavgwidth(exprType(childvar),
1170  exprTypmod(childvar));
1171  Assert(child_width > 0);
1172  parent_attrsizes[pndx] += child_width * childrel->rows;
1173  }
1174  }
1175  }
1176 
1177  if (has_live_children)
1178  {
1179  /*
1180  * Save the finished size estimates.
1181  */
1182  int i;
1183 
1184  Assert(parent_rows > 0);
1185  rel->rows = parent_rows;
1186  rel->reltarget->width = rint(parent_size / parent_rows);
1187  for (i = 0; i < nattrs; i++)
1188  rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1189 
1190  /*
1191  * Set "raw tuples" count equal to "rows" for the appendrel; needed
1192  * because some places assume rel->tuples is valid for any baserel.
1193  */
1194  rel->tuples = parent_rows;
1195 
1196  /*
1197  * Note that we leave rel->pages as zero; this is important to avoid
1198  * double-counting the appendrel tree in total_table_pages.
1199  */
1200  }
1201  else
1202  {
1203  /*
1204  * All children were excluded by constraints, so mark the whole
1205  * appendrel dummy. We must do this in this phase so that the rel's
1206  * dummy-ness is visible when we generate paths for other rels.
1207  */
1209  }
1210 
1211  pfree(parent_attrsizes);
1212 }
bool has_eclass_joins
Definition: pathnodes.h:734
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
RelOptKind reloptkind
Definition: pathnodes.h:663
Relids * attr_needed
Definition: pathnodes.h:699
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
double tuples
Definition: pathnodes.h:706
bool relation_excluded_by_constraints(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: plancat.c:1396
Definition: nodes.h:528
AttrNumber varattno
Definition: primnodes.h:186
void add_child_rel_equivalences(PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
Definition: equivclass.c:2391
Definition: primnodes.h:181
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:639
signed int int32
Definition: c.h:417
void pfree(void *pointer)
Definition: mcxt.c:1057
bool parallelModeOK
Definition: pathnodes.h:137
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:596
static void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:2185
List * joininfo
Definition: pathnodes.h:732
void check_stack_depth(void)
Definition: postgres.c:3377
PlannerGlobal * glob
Definition: pathnodes.h:175
Index relid
Definition: pathnodes.h:694
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
List * exprs
Definition: pathnodes.h:1078
bool consider_partitionwise_join
Definition: pathnodes.h:737
void * palloc0(Size size)
Definition: mcxt.c:981
List * append_rel_list
Definition: pathnodes.h:284
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2498
double rows
Definition: pathnodes.h:669
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
bool enable_partitionwise_join
Definition: costsize.c:145
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
bool consider_parallel
Definition: pathnodes.h:674
#define InvalidAttrNumber
Definition: attnum.h:23
AttrNumber max_attr
Definition: pathnodes.h:698
int i
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1910
Index child_relid
Definition: pathnodes.h:2239
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
Index parent_relid
Definition: pathnodes.h:2238
int32 * attr_widths
Definition: pathnodes.h:700
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:677
static void set_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:366
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
AttrNumber min_attr
Definition: pathnodes.h:697

◆ set_base_rel_consider_startup()

static void set_base_rel_consider_startup ( PlannerInfo root)
static

Definition at line 253 of file allpaths.c.

References bms_get_singleton_member(), RelOptInfo::consider_param_startup, find_base_rel(), JOIN_ANTI, PlannerInfo::join_info_list, JOIN_SEMI, SpecialJoinInfo::jointype, lfirst, and SpecialJoinInfo::syn_righthand.

Referenced by make_one_rel().

254 {
255  /*
256  * Since parameterized paths can only be used on the inside of a nestloop
257  * join plan, there is usually little value in considering fast-start
258  * plans for them. However, for relations that are on the RHS of a SEMI
259  * or ANTI join, a fast-start plan can be useful because we're only going
260  * to care about fetching one tuple anyway.
261  *
262  * To minimize growth of planning time, we currently restrict this to
263  * cases where the RHS is a single base relation, not a join; there is no
264  * provision for consider_param_startup to get set at all on joinrels.
265  * Also we don't worry about appendrels. costsize.c's costing rules for
266  * nestloop semi/antijoins don't consider such cases either.
267  */
268  ListCell *lc;
269 
270  foreach(lc, root->join_info_list)
271  {
272  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
273  int varno;
274 
275  if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
276  bms_get_singleton_member(sjinfo->syn_righthand, &varno))
277  {
278  RelOptInfo *rel = find_base_rel(root, varno);
279 
280  rel->consider_param_startup = true;
281  }
282  }
283 }
List * join_info_list
Definition: pathnodes.h:277
bool consider_param_startup
Definition: pathnodes.h:673
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
Relids syn_righthand
Definition: pathnodes.h:2187
#define lfirst(lc)
Definition: pg_list.h:169
JoinType jointype
Definition: pathnodes.h:2188
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373

◆ set_base_rel_pathlists()

static void set_base_rel_pathlists ( PlannerInfo root)
static

Definition at line 339 of file allpaths.c.

References Assert, RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, set_rel_pathlist(), PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and PlannerInfo::simple_rte_array.

Referenced by make_one_rel().

340 {
341  Index rti;
342 
343  for (rti = 1; rti < root->simple_rel_array_size; rti++)
344  {
345  RelOptInfo *rel = root->simple_rel_array[rti];
346 
347  /* there may be empty slots corresponding to non-baserel RTEs */
348  if (rel == NULL)
349  continue;
350 
351  Assert(rel->relid == rti); /* sanity check on array */
352 
353  /* ignore RTEs that are "other rels" */
354  if (rel->reloptkind != RELOPT_BASEREL)
355  continue;
356 
357  set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
358  }
359 }
RelOptKind reloptkind
Definition: pathnodes.h:663
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
int simple_rel_array_size
Definition: pathnodes.h:198
Index relid
Definition: pathnodes.h:694
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
unsigned int Index
Definition: c.h:537
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:475
#define Assert(condition)
Definition: c.h:792

◆ set_base_rel_sizes()

static void set_base_rel_sizes ( PlannerInfo root)
static

Definition at line 296 of file allpaths.c.

References Assert, PlannerInfo::glob, PlannerGlobal::parallelModeOK, RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, set_rel_consider_parallel(), set_rel_size(), PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and PlannerInfo::simple_rte_array.

Referenced by make_one_rel().

297 {
298  Index rti;
299 
300  for (rti = 1; rti < root->simple_rel_array_size; rti++)
301  {
302  RelOptInfo *rel = root->simple_rel_array[rti];
303  RangeTblEntry *rte;
304 
305  /* there may be empty slots corresponding to non-baserel RTEs */
306  if (rel == NULL)
307  continue;
308 
309  Assert(rel->relid == rti); /* sanity check on array */
310 
311  /* ignore RTEs that are "other rels" */
312  if (rel->reloptkind != RELOPT_BASEREL)
313  continue;
314 
315  rte = root->simple_rte_array[rti];
316 
317  /*
318  * If parallelism is allowable for this query in general, see whether
319  * it's allowable for this rel in particular. We have to do this
320  * before set_rel_size(), because (a) if this rel is an inheritance
321  * parent, set_append_rel_size() will use and perhaps change the rel's
322  * consider_parallel flag, and (b) for some RTE types, set_rel_size()
323  * goes ahead and makes paths immediately.
324  */
325  if (root->glob->parallelModeOK)
326  set_rel_consider_parallel(root, rel, rte);
327 
328  set_rel_size(root, rel, rti, rte);
329  }
330 }
RelOptKind reloptkind
Definition: pathnodes.h:663
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
bool parallelModeOK
Definition: pathnodes.h:137
static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:596
PlannerGlobal * glob
Definition: pathnodes.h:175
int simple_rel_array_size
Definition: pathnodes.h:198
Index relid
Definition: pathnodes.h:694
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
unsigned int Index
Definition: c.h:537
#define Assert(condition)
Definition: c.h:792
static void set_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:366

◆ set_cte_pathlist()

static void set_cte_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2560 of file allpaths.c.

References add_path(), Assert, create_ctescan_path(), PlannerInfo::cte_plan_ids, RangeTblEntry::ctelevelsup, Query::cteList, RangeTblEntry::ctename, CommonTableExpr::ctename, elog, ERROR, PlannerInfo::glob, RelOptInfo::lateral_relids, lfirst, list_length(), list_nth(), list_nth_int(), PlannerInfo::parent_root, PlannerInfo::parse, Plan::plan_rows, set_cte_size_estimates(), and PlannerGlobal::subplans.

Referenced by set_rel_size().

2561 {
2562  Plan *cteplan;
2563  PlannerInfo *cteroot;
2564  Index levelsup;
2565  int ndx;
2566  ListCell *lc;
2567  int plan_id;
2568  Relids required_outer;
2569 
2570  /*
2571  * Find the referenced CTE, and locate the plan previously made for it.
2572  */
2573  levelsup = rte->ctelevelsup;
2574  cteroot = root;
2575  while (levelsup-- > 0)
2576  {
2577  cteroot = cteroot->parent_root;
2578  if (!cteroot) /* shouldn't happen */
2579  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2580  }
2581 
2582  /*
2583  * Note: cte_plan_ids can be shorter than cteList, if we are still working
2584  * on planning the CTEs (ie, this is a side-reference from another CTE).
2585  * So we mustn't use forboth here.
2586  */
2587  ndx = 0;
2588  foreach(lc, cteroot->parse->cteList)
2589  {
2590  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2591 
2592  if (strcmp(cte->ctename, rte->ctename) == 0)
2593  break;
2594  ndx++;
2595  }
2596  if (lc == NULL) /* shouldn't happen */
2597  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2598  if (ndx >= list_length(cteroot->cte_plan_ids))
2599  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2600  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2601  Assert(plan_id > 0);
2602  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2603 
2604  /* Mark rel with estimated output rows, width, etc */
2605  set_cte_size_estimates(root, rel, cteplan->plan_rows);
2606 
2607  /*
2608  * We don't support pushing join clauses into the quals of a CTE scan, but
2609  * it could still have required parameterization due to LATERAL refs in
2610  * its tlist.
2611  */
2612  required_outer = rel->lateral_relids;
2613 
2614  /* Generate appropriate path */
2615  add_path(rel, create_ctescan_path(root, rel, required_outer));
2616 }
double plan_rows
Definition: plannodes.h:123
Query * parse
Definition: pathnodes.h:173
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
PlannerInfo * parent_root
Definition: pathnodes.h:179
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:5378
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:289
Relids lateral_relids
Definition: pathnodes.h:691
#define ERROR
Definition: elog.h:45
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
List * subplans
Definition: pathnodes.h:107
PlannerGlobal * glob
Definition: pathnodes.h:175
List * cte_plan_ids
Definition: pathnodes.h:255
unsigned int Index
Definition: c.h:537
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Index ctelevelsup
Definition: parsenodes.h:1085
List * cteList
Definition: parsenodes.h:135
char * ctename
Definition: parsenodes.h:1084
#define elog(elevel,...)
Definition: elog.h:228
Path * create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2023

◆ set_dummy_rel_pathlist()

static void set_dummy_rel_pathlist ( RelOptInfo rel)
static

Definition at line 2185 of file allpaths.c.

References add_path(), create_append_path(), RelOptInfo::lateral_relids, NIL, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, RelOptInfo::reltarget, RelOptInfo::rows, set_cheapest(), and PathTarget::width.

Referenced by set_append_rel_size(), set_rel_size(), and set_subquery_pathlist().

2186 {
2187  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
2188  rel->rows = 0;
2189  rel->reltarget->width = 0;
2190 
2191  /* Discard any pre-existing paths; no further need for them */
2192  rel->pathlist = NIL;
2193  rel->partial_pathlist = NIL;
2194 
2195  /* Set up the dummy path */
2196  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
2197  NIL, rel->lateral_relids,
2198  0, false, NIL, -1));
2199 
2200  /*
2201  * We set the cheapest-path fields immediately, just in case they were
2202  * pointing at some discarded path. This is redundant when we're called
2203  * from set_rel_size(), but not when called from elsewhere, and doing it
2204  * twice is harmless anyway.
2205  */
2206  set_cheapest(rel);
2207 }
#define NIL
Definition: pg_list.h:65
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
List * partial_pathlist
Definition: pathnodes.h:682
Relids lateral_relids
Definition: pathnodes.h:691
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, List *partitioned_rels, double rows)
Definition: pathnode.c:1215
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
double rows
Definition: pathnodes.h:669
List * pathlist
Definition: pathnodes.h:680
struct PathTarget * reltarget
Definition: pathnodes.h:677

◆ set_foreign_pathlist()

static void set_foreign_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 933 of file allpaths.c.

References RelOptInfo::fdwroutine, FdwRoutine::GetForeignPaths, and RangeTblEntry::relid.

Referenced by set_rel_pathlist().

934 {
935  /* Call the FDW's GetForeignPaths function to generate path(s) */
936  rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
937 }
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:719
GetForeignPaths_function GetForeignPaths
Definition: fdwapi.h:197

◆ set_foreign_size()

static void set_foreign_size ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 909 of file allpaths.c.

References clamp_row_est(), RelOptInfo::fdwroutine, FdwRoutine::GetForeignRelSize, Max, RangeTblEntry::relid, RelOptInfo::rows, set_foreign_size_estimates(), and RelOptInfo::tuples.

Referenced by set_rel_size().

910 {
911  /* Mark rel with estimated output rows, width, etc */
912  set_foreign_size_estimates(root, rel);
913 
914  /* Let FDW adjust the size estimates, if it can */
915  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
916 
917  /* ... but do not let it set the rows estimate to zero */
918  rel->rows = clamp_row_est(rel->rows);
919 
920  /*
921  * Also, make sure rel->tuples is not insane relative to rel->rows.
922  * Notably, this ensures sanity if pg_class.reltuples contains -1 and the
923  * FDW doesn't do anything to replace that.
924  */
925  rel->tuples = Max(rel->tuples, rel->rows);
926 }
double tuples
Definition: pathnodes.h:706
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:719
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:196
double rows
Definition: pathnodes.h:669
#define Max(x, y)
Definition: c.h:968
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5477
double clamp_row_est(double nrows)
Definition: costsize.c:196

◆ set_function_pathlist()

static void set_function_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2448 of file allpaths.c.

References add_path(), build_expression_pathkey(), create_functionscan_path(), PathTarget::exprs, RangeTblEntry::funcordinality, IsA, RelOptInfo::lateral_relids, lfirst, RelOptInfo::max_attr, NIL, RelOptInfo::relid, RelOptInfo::relids, RelOptInfo::reltarget, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by set_rel_pathlist().

2449 {
2450  Relids required_outer;
2451  List *pathkeys = NIL;
2452 
2453  /*
2454  * We don't support pushing join clauses into the quals of a function
2455  * scan, but it could still have required parameterization due to LATERAL
2456  * refs in the function expression.
2457  */
2458  required_outer = rel->lateral_relids;
2459 
2460  /*
2461  * The result is considered unordered unless ORDINALITY was used, in which
2462  * case it is ordered by the ordinal column (the last one). See if we
2463  * care, by checking for uses of that Var in equivalence classes.
2464  */
2465  if (rte->funcordinality)
2466  {
2467  AttrNumber ordattno = rel->max_attr;
2468  Var *var = NULL;
2469  ListCell *lc;
2470 
2471  /*
2472  * Is there a Var for it in rel's targetlist? If not, the query did
2473  * not reference the ordinality column, or at least not in any way
2474  * that would be interesting for sorting.
2475  */
2476  foreach(lc, rel->reltarget->exprs)
2477  {
2478  Var *node = (Var *) lfirst(lc);
2479 
2480  /* checking varno/varlevelsup is just paranoia */
2481  if (IsA(node, Var) &&
2482  node->varattno == ordattno &&
2483  node->varno == rel->relid &&
2484  node->varlevelsup == 0)
2485  {
2486  var = node;
2487  break;
2488  }
2489  }
2490 
2491  /*
2492  * Try to build pathkeys for this Var with int8 sorting. We tell
2493  * build_expression_pathkey not to build any new equivalence class; if
2494  * the Var isn't already mentioned in some EC, it means that nothing
2495  * cares about the ordering.
2496  */
2497  if (var)
2498  pathkeys = build_expression_pathkey(root,
2499  (Expr *) var,
2500  NULL, /* below outer joins */
2501  Int8LessOperator,
2502  rel->relids,
2503  false);
2504  }
2505 
2506  /* Generate appropriate path */
2507  add_path(rel, create_functionscan_path(root, rel,
2508  pathkeys, required_outer));
2509 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid opno, Relids rel, bool create_it)
Definition: pathkeys.c:782
Index varlevelsup
Definition: primnodes.h:191
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
AttrNumber varattno
Definition: primnodes.h:186
bool funcordinality
Definition: parsenodes.h:1069
Definition: primnodes.h:181
Path * create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1945
Relids lateral_relids
Definition: pathnodes.h:691
Relids relids
Definition: pathnodes.h:666
Index relid
Definition: pathnodes.h:694
Index varno
Definition: primnodes.h:184
List * exprs
Definition: pathnodes.h:1078
#define lfirst(lc)
Definition: pg_list.h:169
AttrNumber max_attr
Definition: pathnodes.h:698
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:677
int16 AttrNumber
Definition: attnum.h:21

◆ set_namedtuplestore_pathlist()

static void set_namedtuplestore_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2626 of file allpaths.c.

References add_path(), create_namedtuplestorescan_path(), RelOptInfo::lateral_relids, set_cheapest(), and set_namedtuplestore_size_estimates().

Referenced by set_rel_size().

2628 {
2629  Relids required_outer;
2630 
2631  /* Mark rel with estimated output rows, width, etc */
2633 
2634  /*
2635  * We don't support pushing join clauses into the quals of a tuplestore
2636  * scan, but it could still have required parameterization due to LATERAL
2637  * refs in its tlist.
2638  */
2639  required_outer = rel->lateral_relids;
2640 
2641  /* Generate appropriate path */
2642  add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
2643 
2644  /* Select cheapest path (pretty easy in this case...) */
2645  set_cheapest(rel);
2646 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
Relids lateral_relids
Definition: pathnodes.h:691
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void set_namedtuplestore_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5415
Path * create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2048

◆ set_plain_rel_pathlist()

static void set_plain_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 771 of file allpaths.c.

References add_path(), RelOptInfo::consider_parallel, create_index_paths(), create_plain_partial_paths(), create_seqscan_path(), create_tidscan_paths(), and RelOptInfo::lateral_relids.

Referenced by set_rel_pathlist().

772 {
773  Relids required_outer;
774 
775  /*
776  * We don't support pushing join clauses into the quals of a seqscan, but
777  * it could still have required parameterization due to LATERAL refs in
778  * its tlist.
779  */
780  required_outer = rel->lateral_relids;
781 
782  /* Consider sequential scan */
783  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
784 
785  /* If appropriate, consider parallel sequential scan */
786  if (rel->consider_parallel && required_outer == NULL)
787  create_plain_partial_paths(root, rel);
788 
789  /* Consider index scans */
790  create_index_paths(root, rel);
791 
792  /* Consider TID scans */
793  create_tidscan_paths(root, rel);
794 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
void create_index_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:231
Relids lateral_relids
Definition: pathnodes.h:691
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:385
bool consider_parallel
Definition: pathnodes.h:674
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:801
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:929

◆ set_plain_rel_size()

static void set_plain_rel_size ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 579 of file allpaths.c.

References check_index_predicates(), and set_baserel_size_estimates().

Referenced by set_rel_size().

580 {
581  /*
582  * Test any partial indexes of rel for applicability. We must do this
583  * first since partial unique indexes can affect size estimates.
584  */
585  check_index_predicates(root, rel);
586 
587  /* Mark rel with estimated output rows, width, etc */
588  set_baserel_size_estimates(root, rel);
589 }
void check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:3308
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4651

◆ set_rel_consider_parallel()

static void set_rel_consider_parallel ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 596 of file allpaths.c.

References TableSampleClause::args, Assert, RelOptInfo::baserestrictinfo, castNode, RelOptInfo::consider_parallel, PathTarget::exprs, RelOptInfo::fdwroutine, func_parallel(), RangeTblEntry::functions, get_rel_persistence(), PlannerInfo::glob, is_parallel_safe(), IS_SIMPLE_REL, FdwRoutine::IsForeignScanParallelSafe, limit_needed(), PlannerGlobal::parallelModeOK, RangeTblEntry::relid, RangeTblEntry::relkind, RelOptInfo::reltarget, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::subquery, RangeTblEntry::tablesample, TableSampleClause::tsmhandler, and RangeTblEntry::values_lists.

Referenced by set_append_rel_size(), and set_base_rel_sizes().

598 {
599  /*
600  * The flag has previously been initialized to false, so we can just
601  * return if it becomes clear that we can't safely set it.
602  */
603  Assert(!rel->consider_parallel);
604 
605  /* Don't call this if parallelism is disallowed for the entire query. */
606  Assert(root->glob->parallelModeOK);
607 
608  /* This should only be called for baserels and appendrel children. */
609  Assert(IS_SIMPLE_REL(rel));
610 
611  /* Assorted checks based on rtekind. */
612  switch (rte->rtekind)
613  {
614  case RTE_RELATION:
615 
616  /*
617  * Currently, parallel workers can't access the leader's temporary
618  * tables. We could possibly relax this if we wrote all of its
619  * local buffers at the start of the query and made no changes
620  * thereafter (maybe we could allow hint bit changes), and if we
621  * taught the workers to read them. Writing a large number of
622  * temporary buffers could be expensive, though, and we don't have
623  * the rest of the necessary infrastructure right now anyway. So
624  * for now, bail out if we see a temporary table.
625  */
626  if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
627  return;
628 
629  /*
630  * Table sampling can be pushed down to workers if the sample
631  * function and its arguments are safe.
632  */
633  if (rte->tablesample != NULL)
634  {
635  char proparallel = func_parallel(rte->tablesample->tsmhandler);
636 
637  if (proparallel != PROPARALLEL_SAFE)
638  return;
639  if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
640  return;
641  }
642 
643  /*
644  * Ask FDWs whether they can support performing a ForeignScan
645  * within a worker. Most often, the answer will be no. For
646  * example, if the nature of the FDW is such that it opens a TCP
647  * connection with a remote server, each parallel worker would end
648  * up with a separate connection, and these connections might not
649  * be appropriately coordinated between workers and the leader.
650  */
651  if (rte->relkind == RELKIND_FOREIGN_TABLE)
652  {
653  Assert(rel->fdwroutine);
655  return;
656  if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
657  return;
658  }
659 
660  /*
661  * There are additional considerations for appendrels, which we'll
662  * deal with in set_append_rel_size and set_append_rel_pathlist.
663  * For now, just set consider_parallel based on the rel's own
664  * quals and targetlist.
665  */
666  break;
667 
668  case RTE_SUBQUERY:
669 
670  /*
671  * There's no intrinsic problem with scanning a subquery-in-FROM
672  * (as distinct from a SubPlan or InitPlan) in a parallel worker.
673  * If the subquery doesn't happen to have any parallel-safe paths,
674  * then flagging it as consider_parallel won't change anything,
675  * but that's true for plain tables, too. We must set
676  * consider_parallel based on the rel's own quals and targetlist,
677  * so that if a subquery path is parallel-safe but the quals and
678  * projection we're sticking onto it are not, we correctly mark
679  * the SubqueryScanPath as not parallel-safe. (Note that
680  * set_subquery_pathlist() might push some of these quals down
681  * into the subquery itself, but that doesn't change anything.)
682  *
683  * We can't push sub-select containing LIMIT/OFFSET to workers as
684  * there is no guarantee that the row order will be fully
685  * deterministic, and applying LIMIT/OFFSET will lead to
686  * inconsistent results at the top-level. (In some cases, where
687  * the result is ordered, we could relax this restriction. But it
688  * doesn't currently seem worth expending extra effort to do so.)
689  */
690  {
691  Query *subquery = castNode(Query, rte->subquery);
692 
693  if (limit_needed(subquery))
694  return;
695  }
696  break;
697 
698  case RTE_JOIN:
699  /* Shouldn't happen; we're only considering baserels here. */
700  Assert(false);
701  return;
702 
703  case RTE_FUNCTION:
704  /* Check for parallel-restricted functions. */
705  if (!is_parallel_safe(root, (Node *) rte->functions))
706  return;
707  break;
708 
709  case RTE_TABLEFUNC:
710  /* not parallel safe */
711  return;
712 
713  case RTE_VALUES:
714  /* Check for parallel-restricted functions. */
715  if (!is_parallel_safe(root, (Node *) rte->values_lists))
716  return;
717  break;
718 
719  case RTE_CTE:
720 
721  /*
722  * CTE tuplestores aren't shared among parallel workers, so we
723  * force all CTE scans to happen in the leader. Also, populating
724  * the CTE would require executing a subplan that's not available
725  * in the worker, might be parallel-restricted, and must get
726  * executed only once.
727  */
728  return;
729 
730  case RTE_NAMEDTUPLESTORE:
731 
732  /*
733  * tuplestore cannot be shared, at least without more
734  * infrastructure to support that.
735  */
736  return;
737 
738  case RTE_RESULT:
739  /* RESULT RTEs, in themselves, are no problem. */
740  break;
741  }
742 
743  /*
744  * If there's anything in baserestrictinfo that's parallel-restricted, we
745  * give up on parallelizing access to this relation. We could consider
746  * instead postponing application of the restricted quals until we're
747  * above all the parallelism in the plan tree, but it's not clear that
748  * that would be a win in very many cases, and it might be tricky to make
749  * outer join clauses work correctly. It would likely break equivalence
750  * classes, too.
751  */
752  if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
753  return;
754 
755  /*
756  * Likewise, if the relation's outputs are not parallel-safe, give up.
757  * (Usually, they're just Vars, but sometimes they're not.)
758  */
759  if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
760  return;
761 
762  /* We have a winner. */
763  rel->consider_parallel = true;
764 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:597
List * baserestrictinfo
Definition: pathnodes.h:728
bool limit_needed(Query *parse)
Definition: planner.c:2995
Definition: nodes.h:528
List * values_lists
Definition: parsenodes.h:1079
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:639
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:567
IsForeignScanParallelSafe_function IsForeignScanParallelSafe
Definition: fdwapi.h:250
bool parallelModeOK
Definition: pathnodes.h:137
PlannerGlobal * glob
Definition: pathnodes.h:175
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:719
List * exprs
Definition: pathnodes.h:1078
#define Assert(condition)
Definition: c.h:792
List * functions
Definition: parsenodes.h:1068
char func_parallel(Oid funcid)
Definition: lsyscache.c:1743
bool consider_parallel
Definition: pathnodes.h:674
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:2022
RTEKind rtekind
Definition: parsenodes.h:981
Query * subquery
Definition: parsenodes.h:1016
struct PathTarget * reltarget
Definition: pathnodes.h:677
struct TableSampleClause * tablesample
Definition: parsenodes.h:1011

◆ set_rel_pathlist()

static void set_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 475 of file allpaths.c.

References bms_membership(), BMS_SINGLETON, elog, ERROR, generate_useful_gather_paths(), RangeTblEntry::inh, IS_DUMMY_REL, RangeTblEntry::relkind, RELOPT_BASEREL, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RelOptInfo::rtekind, set_append_rel_pathlist(), set_cheapest(), set_foreign_pathlist(), set_function_pathlist(), set_plain_rel_pathlist(), set_rel_pathlist_hook, set_tablefunc_pathlist(), set_tablesample_rel_pathlist(), set_values_pathlist(), and RangeTblEntry::tablesample.

Referenced by set_append_rel_pathlist(), and set_base_rel_pathlists().

477 {
478  if (IS_DUMMY_REL(rel))
479  {
480  /* We already proved the relation empty, so nothing more to do */
481  }
482  else if (rte->inh)
483  {
484  /* It's an "append relation", process accordingly */
485  set_append_rel_pathlist(root, rel, rti, rte);
486  }
487  else
488  {
489  switch (rel->rtekind)
490  {
491  case RTE_RELATION:
492  if (rte->relkind == RELKIND_FOREIGN_TABLE)
493  {
494  /* Foreign table */
495  set_foreign_pathlist(root, rel, rte);
496  }
497  else if (rte->tablesample != NULL)
498  {
499  /* Sampled relation */
500  set_tablesample_rel_pathlist(root, rel, rte);
501  }
502  else
503  {
504  /* Plain relation */
505  set_plain_rel_pathlist(root, rel, rte);
506  }
507  break;
508  case RTE_SUBQUERY:
509  /* Subquery --- fully handled during set_rel_size */
510  break;
511  case RTE_FUNCTION:
512  /* RangeFunction */
513  set_function_pathlist(root, rel, rte);
514  break;
515  case RTE_TABLEFUNC:
516  /* Table Function */
517  set_tablefunc_pathlist(root, rel, rte);
518  break;
519  case RTE_VALUES:
520  /* Values list */
521  set_values_pathlist(root, rel, rte);
522  break;
523  case RTE_CTE:
524  /* CTE reference --- fully handled during set_rel_size */
525  break;
526  case RTE_NAMEDTUPLESTORE:
527  /* tuplestore reference --- fully handled during set_rel_size */
528  break;
529  case RTE_RESULT:
530  /* simple Result --- fully handled during set_rel_size */
531  break;
532  default:
533  elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
534  break;
535  }
536  }
537 
538  /*
539  * Allow a plugin to editorialize on the set of Paths for this base
540  * relation. It could add new paths (such as CustomPaths) by calling
541  * add_path(), or add_partial_path() if parallel aware. It could also
542  * delete or modify paths added by the core code.
543  */
545  (*set_rel_pathlist_hook) (root, rel, rti, rte);
546 
547  /*
548  * If this is a baserel, we should normally consider gathering any partial
549  * paths we may have created for it. We have to do this after calling the
550  * set_rel_pathlist_hook, else it cannot add partial paths to be included
551  * here.
552  *
553  * However, if this is an inheritance child, skip it. Otherwise, we could
554  * end up with a very large number of gather nodes, each trying to grab
555  * its own pool of workers. Instead, we'll consider gathering partial
556  * paths for the parent appendrel.
557  *
558  * Also, if this is the topmost scan/join rel (that is, the only baserel),
559  * we postpone gathering until the final scan/join targetlist is available
560  * (see grouping_planner).
561  */
562  if (rel->reloptkind == RELOPT_BASEREL &&
563  bms_membership(root->all_baserels) != BMS_SINGLETON)
564  generate_useful_gather_paths(root, rel, false);
565 
566  /* Now find the cheapest of the paths for this rel */
567  set_cheapest(rel);
568 
569 #ifdef OPTIMIZER_DEBUG
570  debug_print_rel(root, rel);
571 #endif
572 }
set_rel_pathlist_hook_type set_rel_pathlist_hook
Definition: allpaths.c:68
void generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:2884
#define ERROR
Definition: elog.h:45
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:1219
static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:933
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:771
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
RTEKind rtekind
Definition: pathnodes.h:696
static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:861
static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2536
#define elog(elevel,...)
Definition: elog.h:228
static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2448
static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2516
struct TableSampleClause * tablesample
Definition: parsenodes.h:1011

◆ set_rel_size()

static void set_rel_size ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 366 of file allpaths.c.

References Assert, elog, ERROR, RangeTblEntry::inh, IS_DUMMY_REL, relation_excluded_by_constraints(), RangeTblEntry::relkind, RELOPT_BASEREL, RelOptInfo::reloptkind, RelOptInfo::rows, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RelOptInfo::rtekind, RangeTblEntry::self_reference, set_append_rel_size(), set_cte_pathlist(), set_dummy_rel_pathlist(), set_foreign_size(), set_function_size_estimates(), set_namedtuplestore_pathlist(), set_plain_rel_size(), set_result_pathlist(), set_subquery_pathlist(), set_tablefunc_size_estimates(), set_tablesample_rel_size(), set_values_size_estimates(), set_worktable_pathlist(), and RangeTblEntry::tablesample.

Referenced by set_append_rel_size(), and set_base_rel_sizes().

368 {
369  if (rel->reloptkind == RELOPT_BASEREL &&
370  relation_excluded_by_constraints(root, rel, rte))
371  {
372  /*
373  * We proved we don't need to scan the rel via constraint exclusion,
374  * so set up a single dummy path for it. Here we only check this for
375  * regular baserels; if it's an otherrel, CE was already checked in
376  * set_append_rel_size().
377  *
378  * In this case, we go ahead and set up the relation's path right away
379  * instead of leaving it for set_rel_pathlist to do. This is because
380  * we don't have a convention for marking a rel as dummy except by
381  * assigning a dummy path to it.
382  */
384  }
385  else if (rte->inh)
386  {
387  /* It's an "append relation", process accordingly */
388  set_append_rel_size(root, rel, rti, rte);
389  }
390  else
391  {
392  switch (rel->rtekind)
393  {
394  case RTE_RELATION:
395  if (rte->relkind == RELKIND_FOREIGN_TABLE)
396  {
397  /* Foreign table */
398  set_foreign_size(root, rel, rte);
399  }
400  else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
401  {
402  /*
403  * We could get here if asked to scan a partitioned table
404  * with ONLY. In that case we shouldn't scan any of the
405  * partitions, so mark it as a dummy rel.
406  */
408  }
409  else if (rte->tablesample != NULL)
410  {
411  /* Sampled relation */
412  set_tablesample_rel_size(root, rel, rte);
413  }
414  else
415  {
416  /* Plain relation */
417  set_plain_rel_size(root, rel, rte);
418  }
419  break;
420  case RTE_SUBQUERY:
421 
422  /*
423  * Subqueries don't support making a choice between
424  * parameterized and unparameterized paths, so just go ahead
425  * and build their paths immediately.
426  */
427  set_subquery_pathlist(root, rel, rti, rte);
428  break;
429  case RTE_FUNCTION:
430  set_function_size_estimates(root, rel);
431  break;
432  case RTE_TABLEFUNC:
433  set_tablefunc_size_estimates(root, rel);
434  break;
435  case RTE_VALUES:
436  set_values_size_estimates(root, rel);
437  break;
438  case RTE_CTE:
439 
440  /*
441  * CTEs don't support making a choice between parameterized
442  * and unparameterized paths, so just go ahead and build their
443  * paths immediately.
444  */
445  if (rte->self_reference)
446  set_worktable_pathlist(root, rel, rte);
447  else
448  set_cte_pathlist(root, rel, rte);
449  break;
450  case RTE_NAMEDTUPLESTORE:
451  /* Might as well just build the path immediately */
452  set_namedtuplestore_pathlist(root, rel, rte);
453  break;
454  case RTE_RESULT:
455  /* Might as well just build the path immediately */
456  set_result_pathlist(root, rel, rte);
457  break;
458  default:
459  elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
460  break;
461  }
462  }
463 
464  /*
465  * We insist that all non-dummy rels have a nonzero rowcount estimate.
466  */
467  Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
468 }
RelOptKind reloptkind
Definition: pathnodes.h:663
static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2560
static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:909
bool relation_excluded_by_constraints(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: plancat.c:1396
static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2626
static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:951
static void set_result_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2656
#define ERROR
Definition: elog.h:45
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
static void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:2185
static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:2244
static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:821
bool self_reference
Definition: parsenodes.h:1086
RTEKind rtekind
Definition: pathnodes.h:696
double rows
Definition: pathnodes.h:669
#define Assert(condition)
Definition: c.h:792
void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5346
static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:579
#define elog(elevel,...)
Definition: elog.h:228
void set_tablefunc_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5324
void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5286
struct TableSampleClause * tablesample
Definition: parsenodes.h:1011
static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2686

◆ set_result_pathlist()

static void set_result_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2656 of file allpaths.c.

References add_path(), create_resultscan_path(), RelOptInfo::lateral_relids, set_cheapest(), and set_result_size_estimates().

Referenced by set_rel_size().

2658 {
2659  Relids required_outer;
2660 
2661  /* Mark rel with estimated output rows, width, etc */
2662  set_result_size_estimates(root, rel);
2663 
2664  /*
2665  * We don't support pushing join clauses into the quals of a Result scan,
2666  * but it could still have required parameterization due to LATERAL refs
2667  * in its tlist.
2668  */
2669  required_outer = rel->lateral_relids;
2670 
2671  /* Generate appropriate path */
2672  add_path(rel, create_resultscan_path(root, rel, required_outer));
2673 
2674  /* Select cheapest path (pretty easy in this case...) */
2675  set_cheapest(rel);
2676 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
Path * create_resultscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2074
void set_result_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5448
Relids lateral_relids
Definition: pathnodes.h:691
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244

◆ set_subquery_pathlist()

static void set_subquery_pathlist ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 2244 of file allpaths.c.

References add_partial_path(), add_path(), Assert, RelOptInfo::baserestrictinfo, bms_is_empty(), RestrictInfo::clause, RelOptInfo::consider_parallel, convert_subquery_pathkeys(), copyObject, create_subqueryscan_path(), Query::distinctClause, fetch_upper_rel(), PlannerInfo::glob, Query::groupClause, Query::groupingSets, has_multiple_baserels(), Query::hasAggs, Query::havingQual, IS_DUMMY_REL, lappend(), RelOptInfo::lateral_relids, lfirst, list_length(), make_tlist_from_pathtarget(), NIL, palloc0(), parse(), PlannerInfo::parse, RelOptInfo::partial_pathlist, Path::pathkeys, RelOptInfo::pathlist, Path::pathtarget, pfree(), PlannerInfo::plan_params, RestrictInfo::pseudoconstant, qual_is_pushdown_safe(), remove_unused_subquery_outputs(), RangeTblEntry::security_barrier, set_dummy_rel_pathlist(), set_subquery_size_estimates(), Query::sortClause, subpath(), RelOptInfo::subplan_params, RangeTblEntry::subquery, subquery_is_pushdown_safe(), subquery_planner(), subquery_push_qual(), RelOptInfo::subroot, Query::targetList, PlannerInfo::tuple_fraction, pushdown_safety_info::unsafeColumns, pushdown_safety_info::unsafeLeaky, and UPPERREL_FINAL.

Referenced by set_rel_size().

2246 {
2247  Query *parse = root->parse;
2248  Query *subquery = rte->subquery;
2249  Relids required_outer;
2250  pushdown_safety_info safetyInfo;
2251  double tuple_fraction;
2252  RelOptInfo *sub_final_rel;
2253  ListCell *lc;
2254 
2255  /*
2256  * Must copy the Query so that planning doesn't mess up the RTE contents
2257  * (really really need to fix the planner to not scribble on its input,
2258  * someday ... but see remove_unused_subquery_outputs to start with).
2259  */
2260  subquery = copyObject(subquery);
2261 
2262  /*
2263  * If it's a LATERAL subquery, it might contain some Vars of the current
2264  * query level, requiring it to be treated as parameterized, even though
2265  * we don't support pushing down join quals into subqueries.
2266  */
2267  required_outer = rel->lateral_relids;
2268 
2269  /*
2270  * Zero out result area for subquery_is_pushdown_safe, so that it can set
2271  * flags as needed while recursing. In particular, we need a workspace
2272  * for keeping track of unsafe-to-reference columns. unsafeColumns[i]
2273  * will be set true if we find that output column i of the subquery is
2274  * unsafe to use in a pushed-down qual.
2275  */
2276  memset(&safetyInfo, 0, sizeof(safetyInfo));
2277  safetyInfo.unsafeColumns = (bool *)
2278  palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
2279 
2280  /*
2281  * If the subquery has the "security_barrier" flag, it means the subquery
2282  * originated from a view that must enforce row level security. Then we
2283  * must not push down quals that contain leaky functions. (Ideally this
2284  * would be checked inside subquery_is_pushdown_safe, but since we don't
2285  * currently pass the RTE to that function, we must do it here.)
2286  */
2287  safetyInfo.unsafeLeaky = rte->security_barrier;
2288 
2289  /*
2290  * If there are any restriction clauses that have been attached to the
2291  * subquery relation, consider pushing them down to become WHERE or HAVING
2292  * quals of the subquery itself. This transformation is useful because it
2293  * may allow us to generate a better plan for the subquery than evaluating
2294  * all the subquery output rows and then filtering them.
2295  *
2296  * There are several cases where we cannot push down clauses. Restrictions
2297  * involving the subquery are checked by subquery_is_pushdown_safe().
2298  * Restrictions on individual clauses are checked by
2299  * qual_is_pushdown_safe(). Also, we don't want to push down
2300  * pseudoconstant clauses; better to have the gating node above the
2301  * subquery.
2302  *
2303  * Non-pushed-down clauses will get evaluated as qpquals of the
2304  * SubqueryScan node.
2305  *
2306  * XXX Are there any cases where we want to make a policy decision not to
2307  * push down a pushable qual, because it'd result in a worse plan?
2308  */
2309  if (rel->baserestrictinfo != NIL &&
2310  subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
2311  {
2312  /* OK to consider pushing down individual quals */
2313  List *upperrestrictlist = NIL;
2314  ListCell *l;
2315 
2316  foreach(l, rel->baserestrictinfo)
2317  {
2318  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2319  Node *clause = (Node *) rinfo->clause;
2320 
2321  if (!rinfo->pseudoconstant &&
2322  qual_is_pushdown_safe(subquery, rti, clause, &safetyInfo))
2323  {
2324  /* Push it down */
2325  subquery_push_qual(subquery, rte, rti, clause);
2326  }
2327  else
2328  {
2329  /* Keep it in the upper query */
2330  upperrestrictlist = lappend(upperrestrictlist, rinfo);
2331  }
2332  }
2333  rel->baserestrictinfo = upperrestrictlist;
2334  /* We don't bother recomputing baserestrict_min_security */
2335  }
2336 
2337  pfree(safetyInfo.unsafeColumns);
2338 
2339  /*
2340  * The upper query might not use all the subquery's output columns; if
2341  * not, we can simplify.
2342  */
2343  remove_unused_subquery_outputs(subquery, rel);
2344 
2345  /*
2346  * We can safely pass the outer tuple_fraction down to the subquery if the
2347  * outer level has no joining, aggregation, or sorting to do. Otherwise
2348  * we'd better tell the subquery to plan for full retrieval. (XXX This
2349  * could probably be made more intelligent ...)
2350  */
2351  if (parse->hasAggs ||
2352  parse->groupClause ||
2353  parse->groupingSets ||
2354  parse->havingQual ||
2355  parse->distinctClause ||
2356  parse->sortClause ||
2357  has_multiple_baserels(root))
2358  tuple_fraction = 0.0; /* default case */
2359  else
2360  tuple_fraction = root->tuple_fraction;
2361 
2362  /* plan_params should not be in use in current query level */
2363  Assert(root->plan_params == NIL);
2364 
2365  /* Generate a subroot and Paths for the subquery */
2366  rel->subroot = subquery_planner(root->glob, subquery,
2367  root,
2368  false, tuple_fraction);
2369 
2370  /* Isolate the params needed by this specific subplan */
2371  rel->subplan_params = root->plan_params;
2372  root->plan_params = NIL;
2373 
2374  /*
2375  * It's possible that constraint exclusion proved the subquery empty. If
2376  * so, it's desirable to produce an unadorned dummy path so that we will
2377  * recognize appropriate optimizations at this query level.
2378  */
2379  sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
2380 
2381  if (IS_DUMMY_REL(sub_final_rel))
2382  {
2384  return;
2385  }
2386 
2387  /*
2388  * Mark rel with estimated output rows, width, etc. Note that we have to
2389  * do this before generating outer-query paths, else cost_subqueryscan is
2390  * not happy.
2391  */
2392  set_subquery_size_estimates(root, rel);
2393 
2394  /*
2395  * For each Path that subquery_planner produced, make a SubqueryScanPath
2396  * in the outer query.
2397  */
2398  foreach(lc, sub_final_rel->pathlist)
2399  {
2400  Path *subpath = (Path *) lfirst(lc);
2401  List *pathkeys;
2402 
2403  /* Convert subpath's pathkeys to outer representation */
2404  pathkeys = convert_subquery_pathkeys(root,
2405  rel,
2406  subpath->pathkeys,
2408 
2409  /* Generate outer path using this subpath */
2410  add_path(rel, (Path *)
2411  create_subqueryscan_path(root, rel, subpath,
2412  pathkeys, required_outer));
2413  }
2414 
2415  /* If outer rel allows parallelism, do same for partial paths. */
2416  if (rel->consider_parallel && bms_is_empty(required_outer))
2417  {
2418  /* If consider_parallel is false, there should be no partial paths. */
2419  Assert(sub_final_rel->consider_parallel ||
2420  sub_final_rel->partial_pathlist == NIL);
2421 
2422  /* Same for partial paths. */
2423  foreach(lc, sub_final_rel->partial_pathlist)
2424  {
2425  Path *subpath = (Path *) lfirst(lc);
2426  List *pathkeys;
2427 
2428  /* Convert subpath's pathkeys to outer representation */
2429  pathkeys = convert_subquery_pathkeys(root,
2430  rel,
2431  subpath->pathkeys,
2433 
2434  /* Generate outer path using this subpath */
2435  add_partial_path(rel, (Path *)
2436  create_subqueryscan_path(root, rel, subpath,
2437  pathkeys,
2438  required_outer));
2439  }
2440  }
2441 }
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5206
#define NIL
Definition: pg_list.h:65
PathTarget * pathtarget
Definition: pathnodes.h:1149
Query * parse
Definition: pathnodes.h:173
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
SubqueryScanPath * create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1917
List * plan_params
Definition: pathnodes.h:187
List * sortClause
Definition: parsenodes.h:158
List * baserestrictinfo
Definition: pathnodes.h:728
bool hasAggs
Definition: parsenodes.h:125
bool pseudoconstant
Definition: pathnodes.h:2002
List * groupingSets
Definition: parsenodes.h:150
Definition: nodes.h:528
List * partial_pathlist
Definition: pathnodes.h:682
List * make_tlist_from_pathtarget(PathTarget *target)
Definition: tlist.c:634
bool * unsafeColumns
Definition: allpaths.c:56
List * targetList
Definition: parsenodes.h:140
PlannerInfo * subroot
Definition: pathnodes.h:710
Relids lateral_relids
Definition: pathnodes.h:691
double tuple_fraction
Definition: pathnodes.h:330
void pfree(void *pointer)
Definition: mcxt.c:1057
List * distinctClause
Definition: parsenodes.h:156
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1188
static void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:2185
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
Definition: pathkeys.c:838
PlannerGlobal * glob
Definition: pathnodes.h:175
static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
Definition: allpaths.c:3743
List * lappend(List *list, void *datum)
Definition: list.c:336
Expr * clause
Definition: pathnodes.h:1994
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3558
void * palloc0(Size size)
Definition: mcxt.c:981
bool security_barrier
Definition: parsenodes.h:1017
List * pathkeys
Definition: pathnodes.h:1162
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
static bool has_multiple_baserels(PlannerInfo *root)
Definition: allpaths.c:2211
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3649
static int list_length(const List *l)
Definition: pg_list.h:149
bool consider_parallel
Definition: pathnodes.h:674
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3292
Query * subquery
Definition: parsenodes.h:1016
List * groupClause
Definition: parsenodes.h:148
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:749
List * pathlist
Definition: pathnodes.h:680
#define copyObject(obj)
Definition: nodes.h:644
Node * havingQual
Definition: parsenodes.h:152
Definition: pg_list.h:50
List * subplan_params
Definition: pathnodes.h:711
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:592
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648
unsigned char bool
Definition: c.h:379

◆ set_tablefunc_pathlist()

static void set_tablefunc_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2536 of file allpaths.c.

References add_path(), create_tablefuncscan_path(), and RelOptInfo::lateral_relids.

Referenced by set_rel_pathlist().

2537 {
2538  Relids required_outer;
2539 
2540  /*
2541  * We don't support pushing join clauses into the quals of a tablefunc
2542  * scan, but it could still have required parameterization due to LATERAL
2543  * refs in the function expression.
2544  */
2545  required_outer = rel->lateral_relids;
2546 
2547  /* Generate appropriate path */
2548  add_path(rel, create_tablefuncscan_path(root, rel,
2549  required_outer));
2550 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
Relids lateral_relids
Definition: pathnodes.h:691
Path * create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1971

◆ set_tablesample_rel_pathlist()

static void set_tablesample_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 861 of file allpaths.c.

References add_path(), PlannerInfo::all_baserels, bms_membership(), BMS_SINGLETON, create_material_path(), create_samplescan_path(), GetTsmRoutine(), RelOptInfo::lateral_relids, PlannerInfo::query_level, TsmRoutine::repeatable_across_scans, RangeTblEntry::tablesample, and TableSampleClause::tsmhandler.

Referenced by set_rel_pathlist().

862 {
863  Relids required_outer;
864  Path *path;
865 
866  /*
867  * We don't support pushing join clauses into the quals of a samplescan,
868  * but it could still have required parameterization due to LATERAL refs
869  * in its tlist or TABLESAMPLE arguments.
870  */
871  required_outer = rel->lateral_relids;
872 
873  /* Consider sampled scan */
874  path = create_samplescan_path(root, rel, required_outer);
875 
876  /*
877  * If the sampling method does not support repeatable scans, we must avoid
878  * plans that would scan the rel multiple times. Ideally, we'd simply
879  * avoid putting the rel on the inside of a nestloop join; but adding such
880  * a consideration to the planner seems like a great deal of complication
881  * to support an uncommon usage of second-rate sampling methods. Instead,
882  * if there is a risk that the query might perform an unsafe join, just
883  * wrap the SampleScan in a Materialize node. We can check for joins by
884  * counting the membership of all_baserels (note that this correctly
885  * counts inheritance trees as single rels). If we're inside a subquery,
886  * we can't easily check whether a join might occur in the outer query, so
887  * just assume one is possible.
888  *
889  * GetTsmRoutine is relatively expensive compared to the other tests here,
890  * so check repeatable_across_scans last, even though that's a bit odd.
891  */
892  if ((root->query_level > 1 ||
895  {
896  path = (Path *) create_material_path(rel, path);
897  }
898 
899  add_path(rel, path);
900 
901  /* For the moment, at least, there are no other paths to consider */
902 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
MaterialPath * create_material_path(RelOptInfo *rel, Path *subpath)
Definition: pathnode.c:1527
Relids lateral_relids
Definition: pathnodes.h:691
Relids all_baserels
Definition: pathnodes.h:221
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
Path * create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:954
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
bool repeatable_across_scans
Definition: tsmapi.h:65
Index query_level
Definition: pathnodes.h:177
struct TableSampleClause * tablesample
Definition: parsenodes.h:1011

◆ set_tablesample_rel_size()

static void set_tablesample_rel_size ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 821 of file allpaths.c.

References TableSampleClause::args, check_index_predicates(), GetTsmRoutine(), RelOptInfo::pages, TsmRoutine::SampleScanGetSampleSize, set_baserel_size_estimates(), RangeTblEntry::tablesample, TableSampleClause::tsmhandler, and RelOptInfo::tuples.

Referenced by set_rel_size().

822 {
823  TableSampleClause *tsc = rte->tablesample;
824  TsmRoutine *tsm;
825  BlockNumber pages;
826  double tuples;
827 
828  /*
829  * Test any partial indexes of rel for applicability. We must do this
830  * first since partial unique indexes can affect size estimates.
831  */
832  check_index_predicates(root, rel);
833 
834  /*
835  * Call the sampling method's estimation function to estimate the number
836  * of pages it will read and the number of tuples it will return. (Note:
837  * we assume the function returns sane values.)
838  */
839  tsm = GetTsmRoutine(tsc->tsmhandler);
840  tsm->SampleScanGetSampleSize(root, rel, tsc->args,
841  &pages, &tuples);
842 
843  /*
844  * For the moment, because we will only consider a SampleScan path for the
845  * rel, it's okay to just overwrite the pages and tuples estimates for the
846  * whole relation. If we ever consider multiple path types for sampled
847  * rels, we'll need more complication.
848  */
849  rel->pages = pages;
850  rel->tuples = tuples;
851 
852  /* Mark rel with estimated output rows, width, etc */
853  set_baserel_size_estimates(root, rel);
854 }
double tuples
Definition: pathnodes.h:706
uint32 BlockNumber
Definition: block.h:31
void check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:3308
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:68
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4651
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
BlockNumber pages
Definition: pathnodes.h:705
struct TableSampleClause * tablesample
Definition: parsenodes.h:1011

◆ set_values_pathlist()

static void set_values_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2516 of file allpaths.c.

References add_path(), create_valuesscan_path(), and RelOptInfo::lateral_relids.

Referenced by set_rel_pathlist().

2517 {
2518  Relids required_outer;
2519 
2520  /*
2521  * We don't support pushing join clauses into the quals of a values scan,
2522  * but it could still have required parameterization due to LATERAL refs
2523  * in the values expressions.
2524  */
2525  required_outer = rel->lateral_relids;
2526 
2527  /* Generate appropriate path */
2528  add_path(rel, create_valuesscan_path(root, rel, required_outer));
2529 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
Path * create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1997
Relids lateral_relids
Definition: pathnodes.h:691

◆ set_worktable_pathlist()

static void set_worktable_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2686 of file allpaths.c.

References add_path(), create_worktablescan_path(), RangeTblEntry::ctelevelsup, RangeTblEntry::ctename, elog, ERROR, RelOptInfo::lateral_relids, PlannerInfo::non_recursive_path, PlannerInfo::parent_root, Path::rows, and set_cte_size_estimates().

Referenced by set_rel_size().

2687 {
2688  Path *ctepath;
2689  PlannerInfo *cteroot;
2690  Index levelsup;
2691  Relids required_outer;
2692 
2693  /*
2694  * We need to find the non-recursive term's path, which is in the plan
2695  * level that's processing the recursive UNION, which is one level *below*
2696  * where the CTE comes from.
2697  */
2698  levelsup = rte->ctelevelsup;
2699  if (levelsup == 0) /* shouldn't happen */
2700  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2701  levelsup--;
2702  cteroot = root;
2703  while (levelsup-- > 0)
2704  {
2705  cteroot = cteroot->parent_root;
2706  if (!cteroot) /* shouldn't happen */
2707  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2708  }
2709  ctepath = cteroot->non_recursive_path;
2710  if (!ctepath) /* shouldn't happen */
2711  elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
2712 
2713  /* Mark rel with estimated output rows, width, etc */
2714  set_cte_size_estimates(root, rel, ctepath->rows);
2715 
2716  /*
2717  * We don't support pushing join clauses into the quals of a worktable
2718  * scan, but it could still have required parameterization due to LATERAL
2719  * refs in its tlist. (I'm not sure this is actually possible given the
2720  * restrictions on recursive references, but it's easy enough to support.)
2721  */
2722  required_outer = rel->lateral_relids;
2723 
2724  /* Generate appropriate path */
2725  add_path(rel, create_worktablescan_path(root, rel, required_outer));
2726 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
PlannerInfo * parent_root
Definition: pathnodes.h:179
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:5378
Relids lateral_relids
Definition: pathnodes.h:691
#define ERROR
Definition: elog.h:45
Path * create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2100
unsigned int Index
Definition: c.h:537
double rows
Definition: pathnodes.h:1158
struct Path * non_recursive_path
Definition: pathnodes.h:358
Index ctelevelsup
Definition: parsenodes.h:1085
char * ctename
Definition: parsenodes.h:1084
#define elog(elevel,...)
Definition: elog.h:228

◆ standard_join_search()

RelOptInfo* standard_join_search ( PlannerInfo root,
int  levels_needed,
List initial_rels 
)

Definition at line 3123 of file allpaths.c.

References Assert, elog, ERROR, generate_partitionwise_join_paths(), generate_useful_gather_paths(), PlannerInfo::join_rel_level, join_search_one_level(), lfirst, linitial, list_length(), NIL, palloc0(), and set_cheapest().

Referenced by make_rel_from_joinlist().

3124 {
3125  int lev;
3126  RelOptInfo *rel;
3127 
3128  /*
3129  * This function cannot be invoked recursively within any one planning
3130  * problem, so join_rel_level[] can't be in use already.
3131  */
3132  Assert(root->join_rel_level == NULL);
3133 
3134  /*
3135  * We employ a simple "dynamic programming" algorithm: we first find all
3136  * ways to build joins of two jointree items, then all ways to build joins
3137  * of three items (from two-item joins and single items), then four-item
3138  * joins, and so on until we have considered all ways to join all the
3139  * items into one rel.
3140  *
3141  * root->join_rel_level[j] is a list of all the j-item rels. Initially we
3142  * set root->join_rel_level[1] to represent all the single-jointree-item
3143  * relations.
3144  */
3145  root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
3146 
3147  root->join_rel_level[1] = initial_rels;
3148 
3149  for (lev = 2; lev <= levels_needed; lev++)
3150  {
3151  ListCell *lc;
3152 
3153  /*
3154  * Determine all possible pairs of relations to be joined at this
3155  * level, and build paths for making each one from every available
3156  * pair of lower-level relations.
3157  */
3158  join_search_one_level(root, lev);
3159 
3160  /*
3161  * Run generate_partitionwise_join_paths() and generate_gather_paths()
3162  * for each just-processed joinrel. We could not do this earlier
3163  * because both regular and partial paths can get added to a
3164  * particular joinrel at multiple times within join_search_one_level.
3165  *
3166  * After that, we're done creating paths for the joinrel, so run
3167  * set_cheapest().
3168  */
3169  foreach(lc, root->join_rel_level[lev])
3170  {
3171  rel = (RelOptInfo *) lfirst(lc);
3172 
3173  /* Create paths for partitionwise joins. */
3175 
3176  /*
3177  * Except for the topmost scan/join rel, consider gathering
3178  * partial paths. We'll do the same for the topmost scan/join rel
3179  * once we know the final targetlist (see grouping_planner).
3180  */
3181  if (lev < levels_needed)
3182  generate_useful_gather_paths(root, rel, false);
3183 
3184  /* Find and save the cheapest paths for this rel */
3185  set_cheapest(rel);
3186 
3187 #ifdef OPTIMIZER_DEBUG
3188  debug_print_rel(root, rel);
3189 #endif
3190  }
3191  }
3192 
3193  /*
3194  * We should have a single rel at the final level.
3195  */
3196  if (root->join_rel_level[levels_needed] == NIL)
3197  elog(ERROR, "failed to build any %d-way joins", levels_needed);
3198  Assert(list_length(root->join_rel_level[levels_needed]) == 1);
3199 
3200  rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
3201 
3202  root->join_rel_level = NULL;
3203 
3204  return rel;
3205 }
#define NIL
Definition: pg_list.h:65
void generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:3972
#define linitial(l)
Definition: pg_list.h:174
void generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:2884
#define ERROR
Definition: elog.h:45
void join_search_one_level(PlannerInfo *root, int level)
Definition: joinrels.c:71
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void * palloc0(Size size)
Definition: mcxt.c:981
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
List ** join_rel_level
Definition: pathnodes.h:250
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:228
Definition: pg_list.h:50

◆ subquery_is_pushdown_safe()

static bool subquery_is_pushdown_safe ( Query subquery,
Query topquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3292 of file allpaths.c.

References Assert, castNode, check_output_expressions(), SetOperationStmt::colTypes, compare_tlist_datatypes(), Query::distinctClause, Query::groupClause, Query::groupingSets, Query::hasTargetSRFs, Query::hasWindowFuncs, Query::limitCount, Query::limitOffset, recurse_pushdown_safe(), Query::setOperations, Query::targetList, and pushdown_safety_info::unsafeVolatile.

Referenced by recurse_pushdown_safe(), and set_subquery_pathlist().

3294 {
3295  SetOperationStmt *topop;
3296 
3297  /* Check point 1 */
3298  if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
3299  return false;
3300 
3301  /* Check point 6 */
3302  if (subquery->groupClause && subquery->groupingSets)
3303  return false;
3304 
3305  /* Check points 3, 4, and 5 */
3306  if (subquery->distinctClause ||
3307  subquery->hasWindowFuncs ||
3308  subquery->hasTargetSRFs)
3309  safetyInfo->unsafeVolatile = true;
3310 
3311  /*
3312  * If we're at a leaf query, check for unsafe expressions in its target
3313  * list, and mark any unsafe ones in unsafeColumns[]. (Non-leaf nodes in
3314  * setop trees have only simple Vars in their tlists, so no need to check
3315  * them.)
3316  */
3317  if (subquery->setOperations == NULL)
3318  check_output_expressions(subquery, safetyInfo);
3319 
3320  /* Are we at top level, or looking at a setop component? */
3321  if (subquery == topquery)
3322  {
3323  /* Top level, so check any component queries */
3324  if (subquery->setOperations != NULL)
3325  if (!recurse_pushdown_safe(subquery->setOperations, topquery,
3326  safetyInfo))
3327  return false;
3328  }
3329  else
3330  {
3331  /* Setop component must not have more components (too weird) */
3332  if (subquery->setOperations != NULL)
3333  return false;
3334  /* Check whether setop component output types match top level */
3335  topop = castNode(SetOperationStmt, topquery->setOperations);
3336  Assert(topop);
3338  topop->colTypes,
3339  safetyInfo);
3340  }
3341  return true;
3342 }
Node * limitOffset
Definition: parsenodes.h:160
#define castNode(_type_, nodeptr)
Definition: nodes.h:597
List * groupingSets
Definition: parsenodes.h:150
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3348
static void compare_tlist_datatypes(List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3483
List * targetList
Definition: parsenodes.h:140
List * distinctClause
Definition: parsenodes.h:156
Node * limitCount
Definition: parsenodes.h:161
static void check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3415
bool hasTargetSRFs
Definition: parsenodes.h:127
#define Assert(condition)
Definition: c.h:792
bool hasWindowFuncs
Definition: parsenodes.h:126
Node * setOperations
Definition: parsenodes.h:166
List * groupClause
Definition: parsenodes.h:148

◆ subquery_push_qual()

static void subquery_push_qual ( Query subquery,
RangeTblEntry rte,
Index  rti,
Node qual 
)
static

Definition at line 3649 of file allpaths.c.

References Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasSubLinks, Query::havingQual, Query::jointree, make_and_qual(), FromExpr::quals, recurse_push_qual(), REPLACEVARS_REPORT_ERROR, ReplaceVarsFromTargetList(), Query::setOperations, and Query::targetList.

Referenced by recurse_push_qual(), and set_subquery_pathlist().

3650 {
3651  if (subquery->setOperations != NULL)
3652  {
3653  /* Recurse to push it separately to each component query */
3654  recurse_push_qual(subquery->setOperations, subquery,
3655  rte, rti, qual);
3656  }
3657  else
3658  {
3659  /*
3660  * We need to replace Vars in the qual (which must refer to outputs of
3661  * the subquery) with copies of the subquery's targetlist expressions.
3662  * Note that at this point, any uplevel Vars in the qual should have
3663  * been replaced with Params, so they need no work.
3664  *
3665  * This step also ensures that when we are pushing into a setop tree,
3666  * each component query gets its own copy of the qual.
3667  */
3668  qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
3669  subquery->targetList,
3671  &subquery->hasSubLinks);
3672 
3673  /*
3674  * Now attach the qual to the proper place: normally WHERE, but if the
3675  * subquery uses grouping or aggregation, put it in HAVING (since the
3676  * qual really refers to the group-result rows).
3677  */
3678  if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
3679  subquery->havingQual = make_and_qual(subquery->havingQual, qual);
3680  else
3681  subquery->jointree->quals =
3682  make_and_qual(subquery->jointree->quals, qual);
3683 
3684  /*
3685  * We need not change the subquery's hasAggs or hasSubLinks flags,
3686  * since we can't be pushing down any aggregates that weren't there
3687  * before, and we don't push down subselects at all.
3688  */
3689  }
3690 }
FromExpr * jointree
Definition: parsenodes.h:138
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: makefuncs.c:689
Node * quals
Definition: primnodes.h:1535
List * targetList
Definition: parsenodes.h:140
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3696
Node * setOperations
Definition: parsenodes.h:166
List * groupClause
Definition: parsenodes.h:148
bool hasSubLinks
Definition: parsenodes.h:128
Node * havingQual
Definition: parsenodes.h:152

◆ targetIsInAllPartitionLists()

static bool targetIsInAllPartitionLists ( TargetEntry tle,
Query query 
)
static

Definition at line 3516 of file allpaths.c.

References InvalidOid, lfirst, WindowClause::partitionClause, targetIsInSortList(), and Query::windowClause.

Referenced by check_output_expressions().

3517 {
3518  ListCell *lc;
3519 
3520  foreach(lc, query->windowClause)
3521  {
3522  WindowClause *wc = (WindowClause *) lfirst(lc);
3523 
3525  return false;
3526  }
3527  return true;
3528 }
List * windowClause
Definition: parsenodes.h:154
List * partitionClause
Definition: parsenodes.h:1358
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define lfirst(lc)
Definition: pg_list.h:169

Variable Documentation

◆ enable_geqo

bool enable_geqo = false

Definition at line 62 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ geqo_threshold

int geqo_threshold

Definition at line 63 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ join_search_hook

join_search_hook_type join_search_hook = NULL

Definition at line 71 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ min_parallel_index_scan_size

int min_parallel_index_scan_size

Definition at line 65 of file allpaths.c.

Referenced by compute_parallel_vacuum_workers(), and compute_parallel_worker().

◆ min_parallel_table_scan_size

int min_parallel_table_scan_size

Definition at line 64 of file allpaths.c.

Referenced by compute_parallel_worker().

◆ set_rel_pathlist_hook

set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL

Definition at line 68 of file allpaths.c.

Referenced by set_rel_pathlist().