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/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/planner.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.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_mergeappend_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 void accumulate_append_subpath (Path *path, List **subpaths, List **special_subpaths)
 
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_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)
 
void set_dummy_rel_pathlist (RelOptInfo *rel)
 
static bool has_multiple_baserels (PlannerInfo *root)
 
void generate_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 
)
static

Definition at line 1972 of file allpaths.c.

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

Referenced by add_paths_to_append_rel(), and generate_mergeappend_paths().

1973 {
1974  if (IsA(path, AppendPath))
1975  {
1976  AppendPath *apath = (AppendPath *) path;
1977 
1978  if (!apath->path.parallel_aware || apath->first_partial_path == 0)
1979  {
1980  /* list_copy is important here to avoid sharing list substructure */
1981  *subpaths = list_concat(*subpaths, list_copy(apath->subpaths));
1982  return;
1983  }
1984  else if (special_subpaths != NULL)
1985  {
1986  List *new_special_subpaths;
1987 
1988  /* Split Parallel Append into partial and non-partial subpaths */
1989  *subpaths = list_concat(*subpaths,
1990  list_copy_tail(apath->subpaths,
1991  apath->first_partial_path));
1992  new_special_subpaths =
1994  apath->first_partial_path);
1995  *special_subpaths = list_concat(*special_subpaths,
1996  new_special_subpaths);
1997  return;
1998  }
1999  }
2000  else if (IsA(path, MergeAppendPath))
2001  {
2002  MergeAppendPath *mpath = (MergeAppendPath *) path;
2003 
2004  /* list_copy is important here to avoid sharing list substructure */
2005  *subpaths = list_concat(*subpaths, list_copy(mpath->subpaths));
2006  return;
2007  }
2008 
2009  *subpaths = lappend(*subpaths, path);
2010 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
List * list_truncate(List *list, int new_size)
Definition: list.c:350
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
Path path
Definition: relation.h:1304
int first_partial_path
Definition: relation.h:1310
List * subpaths
Definition: relation.h:1307
List * lappend(List *list, void *datum)
Definition: list.c:128
bool parallel_aware
Definition: relation.h:1083
List * subpaths
Definition: relation.h:1330
Definition: pg_list.h:45

◆ add_paths_to_append_rel()

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

Definition at line 1377 of file allpaths.c.

References accumulate_append_subpath(), add_partial_path(), add_path(), Assert, bms_equal(), bms_next_member(), RelOptInfo::cheapest_total_path, compare_pathkeys(), RelOptInfo::consider_parallel, create_append_path(), enable_parallel_append, fls(), generate_mergeappend_paths(), get_cheapest_parallel_safe_total_inner(), get_cheapest_parameterized_child_path(), IS_JOIN_REL, IS_SIMPLE_REL, lappend(), lfirst, linitial, list_concat(), list_copy(), list_length(), Max, max_parallel_workers_per_gather, Min, NIL, Path::parallel_workers, Path::param_info, RelOptInfo::part_scheme, RelOptInfo::partial_pathlist, RelOptInfo::partitioned_child_rels, AppendPath::path, PATH_REQ_OUTER, Path::pathkeys, PATHKEYS_EQUAL, RelOptInfo::pathlist, RelOptInfo::relids, Path::rows, RTE_SUBQUERY, RelOptInfo::rtekind, PlannerInfo::simple_rel_array, 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().

1379 {
1380  List *subpaths = NIL;
1381  bool subpaths_valid = true;
1382  List *partial_subpaths = NIL;
1383  List *pa_partial_subpaths = NIL;
1384  List *pa_nonpartial_subpaths = NIL;
1385  bool partial_subpaths_valid = true;
1386  bool pa_subpaths_valid;
1387  List *all_child_pathkeys = NIL;
1388  List *all_child_outers = NIL;
1389  ListCell *l;
1390  List *partitioned_rels = NIL;
1391  bool build_partitioned_rels = false;
1392  double partial_rows = -1;
1393 
1394  /* If appropriate, consider parallel append */
1395  pa_subpaths_valid = enable_parallel_append && rel->consider_parallel;
1396 
1397  /*
1398  * AppendPath generated for partitioned tables must record the RT indexes
1399  * of partitioned tables that are direct or indirect children of this
1400  * Append rel.
1401  *
1402  * AppendPath may be for a sub-query RTE (UNION ALL), in which case, 'rel'
1403  * itself does not represent a partitioned relation, but the child sub-
1404  * queries may contain references to partitioned relations. The loop
1405  * below will look for such children and collect them in a list to be
1406  * passed to the path creation function. (This assumes that we don't need
1407  * to look through multiple levels of subquery RTEs; if we ever do, we
1408  * could consider stuffing the list we generate here into sub-query RTE's
1409  * RelOptInfo, just like we do for partitioned rels, which would be used
1410  * when populating our parent rel with paths. For the present, that
1411  * appears to be unnecessary.)
1412  */
1413  if (rel->part_scheme != NULL)
1414  {
1415  if (IS_SIMPLE_REL(rel))
1416  partitioned_rels = rel->partitioned_child_rels;
1417  else if (IS_JOIN_REL(rel))
1418  {
1419  int relid = -1;
1420 
1421  /*
1422  * For a partitioned joinrel, concatenate the component rels'
1423  * partitioned_child_rels lists.
1424  */
1425  while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1426  {
1427  RelOptInfo *component;
1428 
1429  Assert(relid >= 1 && relid < root->simple_rel_array_size);
1430  component = root->simple_rel_array[relid];
1431  Assert(component->part_scheme != NULL);
1432  Assert(list_length(component->partitioned_child_rels) >= 1);
1433  partitioned_rels =
1434  list_concat(partitioned_rels,
1435  list_copy(component->partitioned_child_rels));
1436  }
1437  }
1438 
1439  Assert(list_length(partitioned_rels) >= 1);
1440  }
1441  else if (rel->rtekind == RTE_SUBQUERY)
1442  build_partitioned_rels = true;
1443 
1444  /*
1445  * For every non-dummy child, remember the cheapest path. Also, identify
1446  * all pathkeys (orderings) and parameterizations (required_outer sets)
1447  * available for the non-dummy member relations.
1448  */
1449  foreach(l, live_childrels)
1450  {
1451  RelOptInfo *childrel = lfirst(l);
1452  ListCell *lcp;
1453  Path *cheapest_partial_path = NULL;
1454 
1455  /*
1456  * If we need to build partitioned_rels, accumulate the partitioned
1457  * rels for this child. We must ensure that parents are always listed
1458  * before their child partitioned tables.
1459  */
1460  if (build_partitioned_rels)
1461  {
1462  List *cprels = childrel->partitioned_child_rels;
1463 
1464  partitioned_rels = list_concat(partitioned_rels,
1465  list_copy(cprels));
1466  }
1467 
1468  /*
1469  * If child has an unparameterized cheapest-total path, add that to
1470  * the unparameterized Append path we are constructing for the parent.
1471  * If not, there's no workable unparameterized path.
1472  *
1473  * With partitionwise aggregates, the child rel's pathlist may be
1474  * empty, so don't assume that a path exists here.
1475  */
1476  if (childrel->pathlist != NIL &&
1477  childrel->cheapest_total_path->param_info == NULL)
1479  &subpaths, NULL);
1480  else
1481  subpaths_valid = false;
1482 
1483  /* Same idea, but for a partial plan. */
1484  if (childrel->partial_pathlist != NIL)
1485  {
1486  cheapest_partial_path = linitial(childrel->partial_pathlist);
1487  accumulate_append_subpath(cheapest_partial_path,
1488  &partial_subpaths, NULL);
1489  }
1490  else
1491  partial_subpaths_valid = false;
1492 
1493  /*
1494  * Same idea, but for a parallel append mixing partial and non-partial
1495  * paths.
1496  */
1497  if (pa_subpaths_valid)
1498  {
1499  Path *nppath = NULL;
1500 
1501  nppath =
1503 
1504  if (cheapest_partial_path == NULL && nppath == NULL)
1505  {
1506  /* Neither a partial nor a parallel-safe path? Forget it. */
1507  pa_subpaths_valid = false;
1508  }
1509  else if (nppath == NULL ||
1510  (cheapest_partial_path != NULL &&
1511  cheapest_partial_path->total_cost < nppath->total_cost))
1512  {
1513  /* Partial path is cheaper or the only option. */
1514  Assert(cheapest_partial_path != NULL);
1515  accumulate_append_subpath(cheapest_partial_path,
1516  &pa_partial_subpaths,
1517  &pa_nonpartial_subpaths);
1518 
1519  }
1520  else
1521  {
1522  /*
1523  * Either we've got only a non-partial path, or we think that
1524  * a single backend can execute the best non-partial path
1525  * faster than all the parallel backends working together can
1526  * execute the best partial path.
1527  *
1528  * It might make sense to be more aggressive here. Even if
1529  * the best non-partial path is more expensive than the best
1530  * partial path, it could still be better to choose the
1531  * non-partial path if there are several such paths that can
1532  * be given to different workers. For now, we don't try to
1533  * figure that out.
1534  */
1536  &pa_nonpartial_subpaths,
1537  NULL);
1538  }
1539  }
1540 
1541  /*
1542  * Collect lists of all the available path orderings and
1543  * parameterizations for all the children. We use these as a
1544  * heuristic to indicate which sort orderings and parameterizations we
1545  * should build Append and MergeAppend paths for.
1546  */
1547  foreach(lcp, childrel->pathlist)
1548  {
1549  Path *childpath = (Path *) lfirst(lcp);
1550  List *childkeys = childpath->pathkeys;
1551  Relids childouter = PATH_REQ_OUTER(childpath);
1552 
1553  /* Unsorted paths don't contribute to pathkey list */
1554  if (childkeys != NIL)
1555  {
1556  ListCell *lpk;
1557  bool found = false;
1558 
1559  /* Have we already seen this ordering? */
1560  foreach(lpk, all_child_pathkeys)
1561  {
1562  List *existing_pathkeys = (List *) lfirst(lpk);
1563 
1564  if (compare_pathkeys(existing_pathkeys,
1565  childkeys) == PATHKEYS_EQUAL)
1566  {
1567  found = true;
1568  break;
1569  }
1570  }
1571  if (!found)
1572  {
1573  /* No, so add it to all_child_pathkeys */
1574  all_child_pathkeys = lappend(all_child_pathkeys,
1575  childkeys);
1576  }
1577  }
1578 
1579  /* Unparameterized paths don't contribute to param-set list */
1580  if (childouter)
1581  {
1582  ListCell *lco;
1583  bool found = false;
1584 
1585  /* Have we already seen this param set? */
1586  foreach(lco, all_child_outers)
1587  {
1588  Relids existing_outers = (Relids) lfirst(lco);
1589 
1590  if (bms_equal(existing_outers, childouter))
1591  {
1592  found = true;
1593  break;
1594  }
1595  }
1596  if (!found)
1597  {
1598  /* No, so add it to all_child_outers */
1599  all_child_outers = lappend(all_child_outers,
1600  childouter);
1601  }
1602  }
1603  }
1604  }
1605 
1606  /*
1607  * If we found unparameterized paths for all children, build an unordered,
1608  * unparameterized Append path for the rel. (Note: this is correct even
1609  * if we have zero or one live subpath due to constraint exclusion.)
1610  */
1611  if (subpaths_valid)
1612  add_path(rel, (Path *) create_append_path(root, rel, subpaths, NIL,
1613  NULL, 0, false,
1614  partitioned_rels, -1));
1615 
1616  /*
1617  * Consider an append of unordered, unparameterized partial paths. Make
1618  * it parallel-aware if possible.
1619  */
1620  if (partial_subpaths_valid)
1621  {
1622  AppendPath *appendpath;
1623  ListCell *lc;
1624  int parallel_workers = 0;
1625 
1626  /* Find the highest number of workers requested for any subpath. */
1627  foreach(lc, partial_subpaths)
1628  {
1629  Path *path = lfirst(lc);
1630 
1631  parallel_workers = Max(parallel_workers, path->parallel_workers);
1632  }
1633  Assert(parallel_workers > 0);
1634 
1635  /*
1636  * If the use of parallel append is permitted, always request at least
1637  * log2(# of children) workers. We assume it can be useful to have
1638  * extra workers in this case because they will be spread out across
1639  * the children. The precise formula is just a guess, but we don't
1640  * want to end up with a radically different answer for a table with N
1641  * partitions vs. an unpartitioned table with the same data, so the
1642  * use of some kind of log-scaling here seems to make some sense.
1643  */
1645  {
1646  parallel_workers = Max(parallel_workers,
1647  fls(list_length(live_childrels)));
1648  parallel_workers = Min(parallel_workers,
1650  }
1651  Assert(parallel_workers > 0);
1652 
1653  /* Generate a partial append path. */
1654  appendpath = create_append_path(root, rel, NIL, partial_subpaths,
1655  NULL, parallel_workers,
1657  partitioned_rels, -1);
1658 
1659  /*
1660  * Make sure any subsequent partial paths use the same row count
1661  * estimate.
1662  */
1663  partial_rows = appendpath->path.rows;
1664 
1665  /* Add the path. */
1666  add_partial_path(rel, (Path *) appendpath);
1667  }
1668 
1669  /*
1670  * Consider a parallel-aware append using a mix of partial and non-partial
1671  * paths. (This only makes sense if there's at least one child which has
1672  * a non-partial path that is substantially cheaper than any partial path;
1673  * otherwise, we should use the append path added in the previous step.)
1674  */
1675  if (pa_subpaths_valid && pa_nonpartial_subpaths != NIL)
1676  {
1677  AppendPath *appendpath;
1678  ListCell *lc;
1679  int parallel_workers = 0;
1680 
1681  /*
1682  * Find the highest number of workers requested for any partial
1683  * subpath.
1684  */
1685  foreach(lc, pa_partial_subpaths)
1686  {
1687  Path *path = lfirst(lc);
1688 
1689  parallel_workers = Max(parallel_workers, path->parallel_workers);
1690  }
1691 
1692  /*
1693  * Same formula here as above. It's even more important in this
1694  * instance because the non-partial paths won't contribute anything to
1695  * the planned number of parallel workers.
1696  */
1697  parallel_workers = Max(parallel_workers,
1698  fls(list_length(live_childrels)));
1699  parallel_workers = Min(parallel_workers,
1701  Assert(parallel_workers > 0);
1702 
1703  appendpath = create_append_path(root, rel, pa_nonpartial_subpaths,
1704  pa_partial_subpaths,
1705  NULL, parallel_workers, true,
1706  partitioned_rels, partial_rows);
1707  add_partial_path(rel, (Path *) appendpath);
1708  }
1709 
1710  /*
1711  * Also build unparameterized MergeAppend paths based on the collected
1712  * list of child pathkeys.
1713  */
1714  if (subpaths_valid)
1715  generate_mergeappend_paths(root, rel, live_childrels,
1716  all_child_pathkeys,
1717  partitioned_rels);
1718 
1719  /*
1720  * Build Append paths for each parameterization seen among the child rels.
1721  * (This may look pretty expensive, but in most cases of practical
1722  * interest, the child rels will expose mostly the same parameterizations,
1723  * so that not that many cases actually get considered here.)
1724  *
1725  * The Append node itself cannot enforce quals, so all qual checking must
1726  * be done in the child paths. This means that to have a parameterized
1727  * Append path, we must have the exact same parameterization for each
1728  * child path; otherwise some children might be failing to check the
1729  * moved-down quals. To make them match up, we can try to increase the
1730  * parameterization of lesser-parameterized paths.
1731  */
1732  foreach(l, all_child_outers)
1733  {
1734  Relids required_outer = (Relids) lfirst(l);
1735  ListCell *lcr;
1736 
1737  /* Select the child paths for an Append with this parameterization */
1738  subpaths = NIL;
1739  subpaths_valid = true;
1740  foreach(lcr, live_childrels)
1741  {
1742  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1743  Path *subpath;
1744 
1745  if (childrel->pathlist == NIL)
1746  {
1747  /* failed to make a suitable path for this child */
1748  subpaths_valid = false;
1749  break;
1750  }
1751 
1753  childrel,
1754  required_outer);
1755  if (subpath == NULL)
1756  {
1757  /* failed to make a suitable path for this child */
1758  subpaths_valid = false;
1759  break;
1760  }
1761  accumulate_append_subpath(subpath, &subpaths, NULL);
1762  }
1763 
1764  if (subpaths_valid)
1765  add_path(rel, (Path *)
1766  create_append_path(root, rel, subpaths, NIL,
1767  required_outer, 0, false,
1768  partitioned_rels, -1));
1769  }
1770 }
#define NIL
Definition: pg_list.h:69
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, Relids required_outer, int parallel_workers, bool parallel_aware, List *partitioned_rels, double rows)
Definition: pathnode.c:1219
static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel, List *live_childrels, List *all_child_pathkeys, List *partitioned_rels)
Definition: allpaths.c:1796
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
static Path * get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: allpaths.c:1883
#define Min(x, y)
Definition: c.h:857
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1075
int parallel_workers
Definition: relation.h:1085
#define IS_JOIN_REL(rel)
Definition: relation.h:590
ParamPathInfo * param_info
Definition: relation.h:1081
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * partial_pathlist
Definition: relation.h:628
List * list_concat(List *list1, List *list2)
Definition: list.c:321
bool enable_parallel_append
Definition: costsize.c:139
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:278
#define IS_SIMPLE_REL(rel)
Definition: relation.h:585
Path path
Definition: relation.h:1304
int fls(int mask)
Definition: fls.c:55
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
#define linitial(l)
Definition: pg_list.h:111
struct Path * cheapest_total_path
Definition: relation.h:630
Relids relids
Definition: relation.h:612
Path * get_cheapest_parallel_safe_total_inner(List *paths)
Definition: pathkeys.c:421
Bitmapset * Relids
Definition: relation.h:29
List * lappend(List *list, void *datum)
Definition: list.c:128
RTEKind rtekind
Definition: relation.h:642
Cost total_cost
Definition: relation.h:1090
List * pathkeys
Definition: relation.h:1092
#define Max(x, y)
Definition: c.h:851
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
double rows
Definition: relation.h:1088
#define PATH_REQ_OUTER(path)
Definition: relation.h:1097
static int list_length(const List *l)
Definition: pg_list.h:89
bool consider_parallel
Definition: relation.h:620
List * partitioned_child_rels
Definition: relation.h:692
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:762
PartitionScheme part_scheme
Definition: relation.h:684
List * pathlist
Definition: relation.h:626
static void accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths)
Definition: allpaths.c:1972
int max_parallel_workers_per_gather
Definition: costsize.c:123
Definition: pg_list.h:45
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:153

◆ check_output_expressions()

static void check_output_expressions ( Query subquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 2988 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().

2989 {
2990  ListCell *lc;
2991 
2992  foreach(lc, subquery->targetList)
2993  {
2994  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2995 
2996  if (tle->resjunk)
2997  continue; /* ignore resjunk columns */
2998 
2999  /* We need not check further if output col is already known unsafe */
3000  if (safetyInfo->unsafeColumns[tle->resno])
3001  continue;
3002 
3003  /* Functions returning sets are unsafe (point 1) */
3004  if (subquery->hasTargetSRFs &&
3005  expression_returns_set((Node *) tle->expr))
3006  {
3007  safetyInfo->unsafeColumns[tle->resno] = true;
3008  continue;
3009  }
3010 
3011  /* Volatile functions are unsafe (point 2) */
3012  if (contain_volatile_functions((Node *) tle->expr))
3013  {
3014  safetyInfo->unsafeColumns[tle->resno] = true;
3015  continue;
3016  }
3017 
3018  /* If subquery uses DISTINCT ON, check point 3 */
3019  if (subquery->hasDistinctOn &&
3020  !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
3021  {
3022  /* non-DISTINCT column, so mark it unsafe */
3023  safetyInfo->unsafeColumns[tle->resno] = true;
3024  continue;
3025  }
3026 
3027  /* If subquery uses window functions, check point 4 */
3028  if (subquery->hasWindowFuncs &&
3029  !targetIsInAllPartitionLists(tle, subquery))
3030  {
3031  /* not present in all PARTITION BY clauses, so mark it unsafe */
3032  safetyInfo->unsafeColumns[tle->resno] = true;
3033  continue;
3034  }
3035  }
3036 }
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:516
bool * unsafeColumns
Definition: allpaths.c:54
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
bool hasDistinctOn
Definition: parsenodes.h:129
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1382
List * distinctClause
Definition: parsenodes.h:156
static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
Definition: allpaths.c:3089
AttrNumber resno
Definition: primnodes.h:1376
#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:106
bool hasWindowFuncs
Definition: parsenodes.h:126
Expr * expr
Definition: primnodes.h:1375

◆ compare_tlist_datatypes()

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

Definition at line 3056 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().

3058 {
3059  ListCell *l;
3060  ListCell *colType = list_head(colTypes);
3061 
3062  foreach(l, tlist)
3063  {
3064  TargetEntry *tle = (TargetEntry *) lfirst(l);
3065 
3066  if (tle->resjunk)
3067  continue; /* ignore resjunk columns */
3068  if (colType == NULL)
3069  elog(ERROR, "wrong number of tlist entries");
3070  if (exprType((Node *) tle->expr) != lfirst_oid(colType))
3071  safetyInfo->unsafeColumns[tle->resno] = true;
3072  colType = lnext(colType);
3073  }
3074  if (colType != NULL)
3075  elog(ERROR, "wrong number of tlist entries");
3076 }
Definition: nodes.h:516
bool * unsafeColumns
Definition: allpaths.c:54
bool resjunk
Definition: primnodes.h:1382
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1376
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1375
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define elog
Definition: elog.h:219
#define lfirst_oid(lc)
Definition: pg_list.h:108

◆ compute_parallel_worker()

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

Definition at line 3443 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().

3445 {
3446  int parallel_workers = 0;
3447 
3448  /*
3449  * If the user has set the parallel_workers reloption, use that; otherwise
3450  * select a default number of workers.
3451  */
3452  if (rel->rel_parallel_workers != -1)
3453  parallel_workers = rel->rel_parallel_workers;
3454  else
3455  {
3456  /*
3457  * If the number of pages being scanned is insufficient to justify a
3458  * parallel scan, just return zero ... unless it's an inheritance
3459  * child. In that case, we want to generate a parallel path here
3460  * anyway. It might not be worthwhile just for this relation, but
3461  * when combined with all of its inheritance siblings it may well pay
3462  * off.
3463  */
3464  if (rel->reloptkind == RELOPT_BASEREL &&
3465  ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3466  (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3467  return 0;
3468 
3469  if (heap_pages >= 0)
3470  {
3471  int heap_parallel_threshold;
3472  int heap_parallel_workers = 1;
3473 
3474  /*
3475  * Select the number of workers based on the log of the size of
3476  * the relation. This probably needs to be a good deal more
3477  * sophisticated, but we need something here for now. Note that
3478  * the upper limit of the min_parallel_table_scan_size GUC is
3479  * chosen to prevent overflow here.
3480  */
3481  heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
3482  while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
3483  {
3484  heap_parallel_workers++;
3485  heap_parallel_threshold *= 3;
3486  if (heap_parallel_threshold > INT_MAX / 3)
3487  break; /* avoid overflow */
3488  }
3489 
3490  parallel_workers = heap_parallel_workers;
3491  }
3492 
3493  if (index_pages >= 0)
3494  {
3495  int index_parallel_workers = 1;
3496  int index_parallel_threshold;
3497 
3498  /* same calculation as for heap_pages above */
3499  index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
3500  while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
3501  {
3502  index_parallel_workers++;
3503  index_parallel_threshold *= 3;
3504  if (index_parallel_threshold > INT_MAX / 3)
3505  break; /* avoid overflow */
3506  }
3507 
3508  if (parallel_workers > 0)
3509  parallel_workers = Min(parallel_workers, index_parallel_workers);
3510  else
3511  parallel_workers = index_parallel_workers;
3512  }
3513  }
3514 
3515  /* In no case use more than caller supplied maximum number of workers */
3516  parallel_workers = Min(parallel_workers, max_workers);
3517 
3518  return parallel_workers;
3519 }
RelOptKind reloptkind
Definition: relation.h:609
#define Min(x, y)
Definition: c.h:857
uint32 BlockNumber
Definition: block.h:31
int min_parallel_index_scan_size
Definition: allpaths.c:63
int rel_parallel_workers
Definition: relation.h:656
#define Max(x, y)
Definition: c.h:851
int min_parallel_table_scan_size
Definition: allpaths.c:62

◆ create_partial_bitmap_paths()

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

Definition at line 3407 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().

3409 {
3410  int parallel_workers;
3411  double pages_fetched;
3412 
3413  /* Compute heap pages for bitmap heap scan */
3414  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
3415  NULL, NULL);
3416 
3417  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1,
3419 
3420  if (parallel_workers <= 0)
3421  return;
3422 
3423  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3424  bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3425 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:3443
Relids lateral_relids
Definition: relation.h:637
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:762
int max_parallel_workers_per_gather
Definition: costsize.c:123
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:5383
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1083

◆ create_plain_partial_paths()

static void create_plain_partial_paths ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 725 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().

726 {
727  int parallel_workers;
728 
729  parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
731 
732  /* If any limit was set to zero, the user doesn't want a parallel scan. */
733  if (parallel_workers <= 0)
734  return;
735 
736  /* Add an unordered partial path based on a parallel sequential scan. */
737  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
738 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:3443
BlockNumber pages
Definition: relation.h:651
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:762
int max_parallel_workers_per_gather
Definition: costsize.c:123
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:954

◆ generate_gather_paths()

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

Definition at line 2550 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 apply_scanjoin_target_to_paths(), gather_grouping_paths(), merge_clump(), set_rel_pathlist(), and standard_join_search().

2551 {
2552  Path *cheapest_partial_path;
2553  Path *simple_gather_path;
2554  ListCell *lc;
2555  double rows;
2556  double *rowsp = NULL;
2557 
2558  /* If there are no partial paths, there's nothing to do here. */
2559  if (rel->partial_pathlist == NIL)
2560  return;
2561 
2562  /* Should we override the rel's rowcount estimate? */
2563  if (override_rows)
2564  rowsp = &rows;
2565 
2566  /*
2567  * The output of Gather is always unsorted, so there's only one partial
2568  * path of interest: the cheapest one. That will be the one at the front
2569  * of partial_pathlist because of the way add_partial_path works.
2570  */
2571  cheapest_partial_path = linitial(rel->partial_pathlist);
2572  rows =
2573  cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
2574  simple_gather_path = (Path *)
2575  create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2576  NULL, rowsp);
2577  add_path(rel, simple_gather_path);
2578 
2579  /*
2580  * For each useful ordering, we can consider an order-preserving Gather
2581  * Merge.
2582  */
2583  foreach(lc, rel->partial_pathlist)
2584  {
2585  Path *subpath = (Path *) lfirst(lc);
2586  GatherMergePath *path;
2587 
2588  if (subpath->pathkeys == NIL)
2589  continue;
2590 
2591  rows = subpath->rows * subpath->parallel_workers;
2592  path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2593  subpath->pathkeys, NULL, rowsp);
2594  add_path(rel, &path->path);
2595  }
2596 }
#define NIL
Definition: pg_list.h:69
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
Definition: pathnode.c:1832
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
int parallel_workers
Definition: relation.h:1085
List * partial_pathlist
Definition: relation.h:628
#define linitial(l)
Definition: pg_list.h:111
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1741
List * pathkeys
Definition: relation.h:1092
#define lfirst(lc)
Definition: pg_list.h:106
double rows
Definition: relation.h:1088
struct PathTarget * reltarget
Definition: relation.h:623
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234

◆ generate_mergeappend_paths()

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

Definition at line 1796 of file allpaths.c.

References accumulate_append_subpath(), add_path(), Assert, RelOptInfo::cheapest_total_path, create_merge_append_path(), get_cheapest_path_for_pathkeys(), lfirst, NIL, Path::param_info, RelOptInfo::pathlist, STARTUP_COST, and TOTAL_COST.

Referenced by add_paths_to_append_rel().

1800 {
1801  ListCell *lcp;
1802 
1803  foreach(lcp, all_child_pathkeys)
1804  {
1805  List *pathkeys = (List *) lfirst(lcp);
1806  List *startup_subpaths = NIL;
1807  List *total_subpaths = NIL;
1808  bool startup_neq_total = false;
1809  ListCell *lcr;
1810 
1811  /* Select the child paths for this ordering... */
1812  foreach(lcr, live_childrels)
1813  {
1814  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1815  Path *cheapest_startup,
1816  *cheapest_total;
1817 
1818  /* Locate the right paths, if they are available. */
1819  cheapest_startup =
1821  pathkeys,
1822  NULL,
1823  STARTUP_COST,
1824  false);
1825  cheapest_total =
1827  pathkeys,
1828  NULL,
1829  TOTAL_COST,
1830  false);
1831 
1832  /*
1833  * If we can't find any paths with the right order just use the
1834  * cheapest-total path; we'll have to sort it later.
1835  */
1836  if (cheapest_startup == NULL || cheapest_total == NULL)
1837  {
1838  cheapest_startup = cheapest_total =
1839  childrel->cheapest_total_path;
1840  /* Assert we do have an unparameterized path for this child */
1841  Assert(cheapest_total->param_info == NULL);
1842  }
1843 
1844  /*
1845  * Notice whether we actually have different paths for the
1846  * "cheapest" and "total" cases; frequently there will be no point
1847  * in two create_merge_append_path() calls.
1848  */
1849  if (cheapest_startup != cheapest_total)
1850  startup_neq_total = true;
1851 
1852  accumulate_append_subpath(cheapest_startup,
1853  &startup_subpaths, NULL);
1854  accumulate_append_subpath(cheapest_total,
1855  &total_subpaths, NULL);
1856  }
1857 
1858  /* ... and build the MergeAppend paths */
1859  add_path(rel, (Path *) create_merge_append_path(root,
1860  rel,
1861  startup_subpaths,
1862  pathkeys,
1863  NULL,
1864  partitioned_rels));
1865  if (startup_neq_total)
1866  add_path(rel, (Path *) create_merge_append_path(root,
1867  rel,
1868  total_subpaths,
1869  pathkeys,
1870  NULL,
1871  partitioned_rels));
1872  }
1873 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:343
#define NIL
Definition: pg_list.h:69
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
ParamPathInfo * param_info
Definition: relation.h:1081
struct Path * cheapest_total_path
Definition: relation.h:630
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer, List *partitioned_rels)
Definition: pathnode.c:1347
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * pathlist
Definition: relation.h:626
static void accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths)
Definition: allpaths.c:1972
Definition: pg_list.h:45

◆ generate_partitionwise_join_paths()

void generate_partitionwise_join_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 3531 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, 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(), RelOptInfo::relids, RelOptInfo::reltarget, RelOptInfo::rows, Path::rows, Query::rtable, set_cheapest(), PlannerInfo::simple_rte_array, Path::startup_cost, subpath(), T_AggPath, T_AppendPath, T_BitmapAndPath, T_BitmapHeapPath, T_BitmapOrPath, T_CteScan, T_ForeignPath, T_FunctionScan, T_GatherPath, T_GroupingSetsPath, T_GroupPath, T_HashPath, T_IndexPath, T_LimitPath, T_LockRowsPath, T_MaterialPath, T_MergeAppendPath, T_MergePath, T_MinMaxAggPath, T_ModifyTablePath, T_NestPath, T_Path, T_ProjectionPath, T_ProjectSetPath, T_RecursiveUnionPath, T_ResultPath, T_SampleScan, T_SeqScan, T_SetOpPath, T_SortPath, T_SubqueryScan, 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().

3532 {
3533  List *live_children = NIL;
3534  int cnt_parts;
3535  int num_parts;
3536  RelOptInfo **part_rels;
3537 
3538  /* Handle only join relations here. */
3539  if (!IS_JOIN_REL(rel))
3540  return;
3541 
3542  /* We've nothing to do if the relation is not partitioned. */
3543  if (!IS_PARTITIONED_REL(rel))
3544  return;
3545 
3546  /* Guard against stack overflow due to overly deep partition hierarchy. */
3548 
3549  num_parts = rel->nparts;
3550  part_rels = rel->part_rels;
3551 
3552  /* Collect non-dummy child-joins. */
3553  for (cnt_parts = 0; cnt_parts < num_parts; cnt_parts++)
3554  {
3555  RelOptInfo *child_rel = part_rels[cnt_parts];
3556 
3557  Assert(child_rel != NULL);
3558 
3559  /* Add partitionwise join paths for partitioned child-joins. */
3560  generate_partitionwise_join_paths(root, child_rel);
3561 
3562  /* Dummy children will not be scanned, so ignore those. */
3563  if (IS_DUMMY_REL(child_rel))
3564  continue;
3565 
3566  set_cheapest(child_rel);
3567 
3568 #ifdef OPTIMIZER_DEBUG
3569  debug_print_rel(root, child_rel);
3570 #endif
3571 
3572  live_children = lappend(live_children, child_rel);
3573  }
3574 
3575  /* If all child-joins are dummy, parent join is also dummy. */
3576  if (!live_children)
3577  {
3578  mark_dummy_rel(rel);
3579  return;
3580  }
3581 
3582  /* Build additional paths for this rel from child-join paths. */
3583  add_paths_to_append_rel(root, rel, live_children);
3584  list_free(live_children);
3585 }
#define NIL
Definition: pg_list.h:69
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1377
#define IS_PARTITIONED_REL(rel)
Definition: relation.h:706
#define IS_JOIN_REL(rel)
Definition: relation.h:590
void generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:3531
#define IS_DUMMY_REL(r)
Definition: relation.h:1317
void check_stack_depth(void)
Definition: postgres.c:3159
int nparts
Definition: relation.h:685
List * lappend(List *list, void *datum)
Definition: list.c:128
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1215
#define Assert(condition)
Definition: c.h:699
struct RelOptInfo ** part_rels
Definition: relation.h:688
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45

◆ get_cheapest_parameterized_child_path()

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

Definition at line 1883 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().

1885 {
1886  Path *cheapest;
1887  ListCell *lc;
1888 
1889  /*
1890  * Look up the cheapest existing path with no more than the needed
1891  * parameterization. If it has exactly the needed parameterization, we're
1892  * done.
1893  */
1894  cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
1895  NIL,
1896  required_outer,
1897  TOTAL_COST,
1898  false);
1899  Assert(cheapest != NULL);
1900  if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
1901  return cheapest;
1902 
1903  /*
1904  * Otherwise, we can "reparameterize" an existing path to match the given
1905  * parameterization, which effectively means pushing down additional
1906  * joinquals to be checked within the path's scan. However, some existing
1907  * paths might check the available joinquals already while others don't;
1908  * therefore, it's not clear which existing path will be cheapest after
1909  * reparameterization. We have to go through them all and find out.
1910  */
1911  cheapest = NULL;
1912  foreach(lc, rel->pathlist)
1913  {
1914  Path *path = (Path *) lfirst(lc);
1915 
1916  /* Can't use it if it needs more than requested parameterization */
1917  if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
1918  continue;
1919 
1920  /*
1921  * Reparameterization can only increase the path's cost, so if it's
1922  * already more expensive than the current cheapest, forget it.
1923  */
1924  if (cheapest != NULL &&
1925  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1926  continue;
1927 
1928  /* Reparameterize if needed, then recheck cost */
1929  if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
1930  {
1931  path = reparameterize_path(root, path, required_outer, 1.0);
1932  if (path == NULL)
1933  continue; /* failed to reparameterize this one */
1934  Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
1935 
1936  if (cheapest != NULL &&
1937  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1938  continue;
1939  }
1940 
1941  /* We have a new best path */
1942  cheapest = path;
1943  }
1944 
1945  /* Return the best path, or NULL if we found no suitable candidate */
1946  return cheapest;
1947 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:343
#define NIL
Definition: pg_list.h:69
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:71
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:1097
Path * reparameterize_path(PlannerInfo *root, Path *path, Relids required_outer, double loop_count)
Definition: pathnode.c:3513
List * pathlist
Definition: relation.h:626
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:153

◆ has_multiple_baserels()

static bool has_multiple_baserels ( PlannerInfo root)
static

Definition at line 2046 of file allpaths.c.

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

Referenced by set_subquery_pathlist().

2047 {
2048  int num_base_rels = 0;
2049  Index rti;
2050 
2051  for (rti = 1; rti < root->simple_rel_array_size; rti++)
2052  {
2053  RelOptInfo *brel = root->simple_rel_array[rti];
2054 
2055  if (brel == NULL)
2056  continue;
2057 
2058  /* ignore RTEs that are "other rels" */
2059  if (brel->reloptkind == RELOPT_BASEREL)
2060  if (++num_base_rels > 1)
2061  return true;
2062  }
2063  return false;
2064 }
RelOptKind reloptkind
Definition: relation.h:609
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
int simple_rel_array_size
Definition: relation.h:194
unsigned int Index
Definition: c.h:442

◆ make_one_rel()

RelOptInfo* make_one_rel ( PlannerInfo root,
List joinlist 
)

Definition at line 146 of file allpaths.c.

References PlannerInfo::all_baserels, Assert, bms_add_member(), bms_equal(), make_rel_from_joinlist(), 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, and PlannerInfo::simple_rel_array_size.

Referenced by query_planner().

147 {
148  RelOptInfo *rel;
149  Index rti;
150 
151  /*
152  * Construct the all_baserels Relids set.
153  */
154  root->all_baserels = NULL;
155  for (rti = 1; rti < root->simple_rel_array_size; rti++)
156  {
157  RelOptInfo *brel = root->simple_rel_array[rti];
158 
159  /* there may be empty slots corresponding to non-baserel RTEs */
160  if (brel == NULL)
161  continue;
162 
163  Assert(brel->relid == rti); /* sanity check on array */
164 
165  /* ignore RTEs that are "other rels" */
166  if (brel->reloptkind != RELOPT_BASEREL)
167  continue;
168 
169  root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
170  }
171 
172  /* Mark base rels as to whether we care about fast-start plans */
174 
175  /*
176  * Compute size estimates and consider_parallel flags for each base rel,
177  * then generate access paths.
178  */
179  set_base_rel_sizes(root);
181 
182  /*
183  * Generate access paths for the entire join tree.
184  */
185  rel = make_rel_from_joinlist(root, joinlist);
186 
187  /*
188  * The result should join all and only the query's base rels.
189  */
190  Assert(bms_equal(rel->relids, root->all_baserels));
191 
192  return rel;
193 }
RelOptKind reloptkind
Definition: relation.h:609
static void set_base_rel_sizes(PlannerInfo *root)
Definition: allpaths.c:249
static void set_base_rel_consider_startup(PlannerInfo *root)
Definition: allpaths.c:206
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
Relids all_baserels
Definition: relation.h:210
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:2606
Relids relids
Definition: relation.h:612
int simple_rel_array_size
Definition: relation.h:194
Index relid
Definition: relation.h:640
static void set_base_rel_pathlists(PlannerInfo *root)
Definition: allpaths.c:292
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:153

◆ make_rel_from_joinlist()

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

Definition at line 2606 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().

2607 {
2608  int levels_needed;
2609  List *initial_rels;
2610  ListCell *jl;
2611 
2612  /*
2613  * Count the number of child joinlist nodes. This is the depth of the
2614  * dynamic-programming algorithm we must employ to consider all ways of
2615  * joining the child nodes.
2616  */
2617  levels_needed = list_length(joinlist);
2618 
2619  if (levels_needed <= 0)
2620  return NULL; /* nothing to do? */
2621 
2622  /*
2623  * Construct a list of rels corresponding to the child joinlist nodes.
2624  * This may contain both base rels and rels constructed according to
2625  * sub-joinlists.
2626  */
2627  initial_rels = NIL;
2628  foreach(jl, joinlist)
2629  {
2630  Node *jlnode = (Node *) lfirst(jl);
2631  RelOptInfo *thisrel;
2632 
2633  if (IsA(jlnode, RangeTblRef))
2634  {
2635  int varno = ((RangeTblRef *) jlnode)->rtindex;
2636 
2637  thisrel = find_base_rel(root, varno);
2638  }
2639  else if (IsA(jlnode, List))
2640  {
2641  /* Recurse to handle subproblem */
2642  thisrel = make_rel_from_joinlist(root, (List *) jlnode);
2643  }
2644  else
2645  {
2646  elog(ERROR, "unrecognized joinlist node type: %d",
2647  (int) nodeTag(jlnode));
2648  thisrel = NULL; /* keep compiler quiet */
2649  }
2650 
2651  initial_rels = lappend(initial_rels, thisrel);
2652  }
2653 
2654  if (levels_needed == 1)
2655  {
2656  /*
2657  * Single joinlist node, so we're done.
2658  */
2659  return (RelOptInfo *) linitial(initial_rels);
2660  }
2661  else
2662  {
2663  /*
2664  * Consider the different orders in which we could join the rels,
2665  * using a plugin, GEQO, or the regular join search code.
2666  *
2667  * We put the initial_rels list into a PlannerInfo field because
2668  * has_legal_joinclause() needs to look at it (ugly :-().
2669  */
2670  root->initial_rels = initial_rels;
2671 
2672  if (join_search_hook)
2673  return (*join_search_hook) (root, levels_needed, initial_rels);
2674  else if (enable_geqo && levels_needed >= geqo_threshold)
2675  return geqo(root, levels_needed, initial_rels);
2676  else
2677  return standard_join_search(root, levels_needed, initial_rels);
2678  }
2679 }
RelOptInfo * standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
Definition: allpaths.c:2711
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
Definition: nodes.h:516
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
bool enable_geqo
Definition: allpaths.c:60
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:2606
List * lappend(List *list, void *datum)
Definition: list.c:128
join_search_hook_type join_search_hook
Definition: allpaths.c:69
int geqo_threshold
Definition: allpaths.c:61
RelOptInfo * geqo(PlannerInfo *root, int number_of_rels, List *initial_rels)
Definition: geqo_main.c:67
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
#define nodeTag(nodeptr)
Definition: nodes.h:521
List * initial_rels
Definition: relation.h:284
#define elog
Definition: elog.h:219
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:279
Definition: pg_list.h:45

◆ qual_is_pushdown_safe()

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

Definition at line 3131 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().

3133 {
3134  bool safe = true;
3135  List *vars;
3136  ListCell *vl;
3137 
3138  /* Refuse subselects (point 1) */
3139  if (contain_subplans(qual))
3140  return false;
3141 
3142  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
3143  if (safetyInfo->unsafeVolatile &&
3145  return false;
3146 
3147  /* Refuse leaky quals if told to (point 3) */
3148  if (safetyInfo->unsafeLeaky &&
3149  contain_leaked_vars(qual))
3150  return false;
3151 
3152  /*
3153  * It would be unsafe to push down window function calls, but at least for
3154  * the moment we could never see any in a qual anyhow. (The same applies
3155  * to aggregates, which we check for in pull_var_clause below.)
3156  */
3158 
3159  /*
3160  * Examine all Vars used in clause; since it's a restriction clause, all
3161  * such Vars must refer to subselect output columns.
3162  */
3164  foreach(vl, vars)
3165  {
3166  Var *var = (Var *) lfirst(vl);
3167 
3168  /*
3169  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
3170  * It's not clear whether a PHV could safely be pushed down, and even
3171  * less clear whether such a situation could arise in any cases of
3172  * practical interest anyway. So for the moment, just refuse to push
3173  * down.
3174  */
3175  if (!IsA(var, Var))
3176  {
3177  safe = false;
3178  break;
3179  }
3180 
3181  Assert(var->varno == rti);
3182  Assert(var->varattno >= 0);
3183 
3184  /* Check point 4 */
3185  if (var->varattno == 0)
3186  {
3187  safe = false;
3188  break;
3189  }
3190 
3191  /* Check point 5 */
3192  if (safetyInfo->unsafeColumns[var->varattno])
3193  {
3194  safe = false;
3195  break;
3196  }
3197  }
3198 
3199  list_free(vars);
3200 
3201  return safe;
3202 }
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1516
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
AttrNumber varattno
Definition: primnodes.h:168
bool * unsafeColumns
Definition: allpaths.c:54
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
Definition: primnodes.h:163
bool contain_subplans(Node *clause)
Definition: clauses.c:844
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
bool contain_window_function(Node *clause)
Definition: clauses.c:728
Index varno
Definition: primnodes.h:166
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
void list_free(List *list)
Definition: list.c:1133
Definition: regcomp.c:224
Definition: pg_list.h:45

◆ recurse_push_qual()

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

Definition at line 3255 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().

3257 {
3258  if (IsA(setOp, RangeTblRef))
3259  {
3260  RangeTblRef *rtr = (RangeTblRef *) setOp;
3261  RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
3262  Query *subquery = subrte->subquery;
3263 
3264  Assert(subquery != NULL);
3265  subquery_push_qual(subquery, rte, rti, qual);
3266  }
3267  else if (IsA(setOp, SetOperationStmt))
3268  {
3269  SetOperationStmt *op = (SetOperationStmt *) setOp;
3270 
3271  recurse_push_qual(op->larg, topquery, rte, rti, qual);
3272  recurse_push_qual(op->rarg, topquery, rte, rti, qual);
3273  }
3274  else
3275  {
3276  elog(ERROR, "unrecognized node type: %d",
3277  (int) nodeTag(setOp));
3278  }
3279 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:699
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3208
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3255
#define nodeTag(nodeptr)
Definition: nodes.h:521
Query * subquery
Definition: parsenodes.h:985
#define elog
Definition: elog.h:219

◆ recurse_pushdown_safe()

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

Definition at line 2921 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().

2923 {
2924  if (IsA(setOp, RangeTblRef))
2925  {
2926  RangeTblRef *rtr = (RangeTblRef *) setOp;
2927  RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
2928  Query *subquery = rte->subquery;
2929 
2930  Assert(subquery != NULL);
2931  return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
2932  }
2933  else if (IsA(setOp, SetOperationStmt))
2934  {
2935  SetOperationStmt *op = (SetOperationStmt *) setOp;
2936 
2937  /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
2938  if (op->op == SETOP_EXCEPT)
2939  return false;
2940  /* Else recurse */
2941  if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
2942  return false;
2943  if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
2944  return false;
2945  }
2946  else
2947  {
2948  elog(ERROR, "unrecognized node type: %d",
2949  (int) nodeTag(setOp));
2950  }
2951  return true;
2952 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2921
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:699
SetOperation op
Definition: parsenodes.h:1599
#define nodeTag(nodeptr)
Definition: nodes.h:521
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2869
Query * subquery
Definition: parsenodes.h:985
#define elog
Definition: elog.h:219

◆ remove_unused_subquery_outputs()

static void remove_unused_subquery_outputs ( Query subquery,
RelOptInfo rel 
)
static

Definition at line 3302 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().

3303 {
3304  Bitmapset *attrs_used = NULL;
3305  ListCell *lc;
3306 
3307  /*
3308  * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
3309  * could update all the child SELECTs' tlists, but it seems not worth the
3310  * trouble presently.
3311  */
3312  if (subquery->setOperations)
3313  return;
3314 
3315  /*
3316  * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
3317  * time: all its output columns must be used in the distinctClause.
3318  */
3319  if (subquery->distinctClause && !subquery->hasDistinctOn)
3320  return;
3321 
3322  /*
3323  * Collect a bitmap of all the output column numbers used by the upper
3324  * query.
3325  *
3326  * Add all the attributes needed for joins or final output. Note: we must
3327  * look at rel's targetlist, not the attr_needed data, because attr_needed
3328  * isn't computed for inheritance child rels, cf set_append_rel_size().
3329  * (XXX might be worth changing that sometime.)
3330  */
3331  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
3332 
3333  /* Add all the attributes used by un-pushed-down restriction clauses. */
3334  foreach(lc, rel->baserestrictinfo)
3335  {
3336  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3337 
3338  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
3339  }
3340 
3341  /*
3342  * If there's a whole-row reference to the subquery, we can't remove
3343  * anything.
3344  */
3346  return;
3347 
3348  /*
3349  * Run through the tlist and zap entries we don't need. It's okay to
3350  * modify the tlist items in-place because set_subquery_pathlist made a
3351  * copy of the subquery.
3352  */
3353  foreach(lc, subquery->targetList)
3354  {
3355  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3356  Node *texpr = (Node *) tle->expr;
3357 
3358  /*
3359  * If it has a sortgroupref number, it's used in some sort/group
3360  * clause so we'd better not remove it. Also, don't remove any
3361  * resjunk columns, since their reason for being has nothing to do
3362  * with anybody reading the subquery's output. (It's likely that
3363  * resjunk columns in a sub-SELECT would always have ressortgroupref
3364  * set, but even if they don't, it seems imprudent to remove them.)
3365  */
3366  if (tle->ressortgroupref || tle->resjunk)
3367  continue;
3368 
3369  /*
3370  * If it's used by the upper query, we can't remove it.
3371  */
3373  attrs_used))
3374  continue;
3375 
3376  /*
3377  * If it contains a set-returning function, we can't remove it since
3378  * that could change the number of rows returned by the subquery.
3379  */
3380  if (subquery->hasTargetSRFs &&
3381  expression_returns_set(texpr))
3382  continue;
3383 
3384  /*
3385  * If it contains volatile functions, we daren't remove it for fear
3386  * that the user is expecting their side-effects to happen.
3387  */
3388  if (contain_volatile_functions(texpr))
3389  continue;
3390 
3391  /*
3392  * OK, we don't need it. Replace the expression with a NULL constant.
3393  * Preserve the exposed type of the expression, in case something
3394  * looks at the rowtype of the subquery's result.
3395  */
3396  tle->expr = (Expr *) makeNullConst(exprType(texpr),
3397  exprTypmod(texpr),
3398  exprCollation(texpr));
3399  }
3400 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
List * baserestrictinfo
Definition: relation.h:672
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:516
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
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:336
bool resjunk
Definition: primnodes.h:1382
List * distinctClause
Definition: parsenodes.h:156
AttrNumber resno
Definition: primnodes.h:1376
Index relid
Definition: relation.h:640
Expr * clause
Definition: relation.h:1880
List * exprs
Definition: relation.h:1008
bool hasTargetSRFs
Definition: parsenodes.h:127
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1375
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
Node * setOperations
Definition: parsenodes.h:165
Index ressortgroupref
Definition: primnodes.h:1378
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
struct PathTarget * reltarget
Definition: relation.h:623

◆ set_append_rel_pathlist()

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

Definition at line 1301 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, list_concat(), list_copy(), NIL, AppendRelInfo::parent_relid, RelOptInfo::part_scheme, RelOptInfo::partitioned_child_rels, set_rel_pathlist(), PlannerInfo::simple_rel_array, and PlannerInfo::simple_rte_array.

Referenced by set_rel_pathlist().

1303 {
1304  int parentRTindex = rti;
1305  List *live_childrels = NIL;
1306  ListCell *l;
1307 
1308  /*
1309  * Generate access paths for each member relation, and remember the
1310  * non-dummy children.
1311  */
1312  foreach(l, root->append_rel_list)
1313  {
1314  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1315  int childRTindex;
1316  RangeTblEntry *childRTE;
1317  RelOptInfo *childrel;
1318 
1319  /* append_rel_list contains all append rels; ignore others */
1320  if (appinfo->parent_relid != parentRTindex)
1321  continue;
1322 
1323  /* Re-locate the child RTE and RelOptInfo */
1324  childRTindex = appinfo->child_relid;
1325  childRTE = root->simple_rte_array[childRTindex];
1326  childrel = root->simple_rel_array[childRTindex];
1327 
1328  /*
1329  * If set_append_rel_size() decided the parent appendrel was
1330  * parallel-unsafe at some point after visiting this child rel, we
1331  * need to propagate the unsafety marking down to the child, so that
1332  * we don't generate useless partial paths for it.
1333  */
1334  if (!rel->consider_parallel)
1335  childrel->consider_parallel = false;
1336 
1337  /*
1338  * Compute the child's access paths.
1339  */
1340  set_rel_pathlist(root, childrel, childRTindex, childRTE);
1341 
1342  /*
1343  * If child is dummy, ignore it.
1344  */
1345  if (IS_DUMMY_REL(childrel))
1346  continue;
1347 
1348  /* Bubble up childrel's partitioned children. */
1349  if (rel->part_scheme)
1350  rel->partitioned_child_rels =
1352  list_copy(childrel->partitioned_child_rels));
1353 
1354  /*
1355  * Child is live, so add it to the live_childrels list for use below.
1356  */
1357  live_childrels = lappend(live_childrels, childrel);
1358  }
1359 
1360  /* Add paths to the append relation. */
1361  add_paths_to_append_rel(root, rel, live_childrels);
1362 }
#define NIL
Definition: pg_list.h:69
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1377
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_concat(List *list1, List *list2)
Definition: list.c:321
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
#define IS_DUMMY_REL(r)
Definition: relation.h:1317
List * lappend(List *list, void *datum)
Definition: list.c:128
RangeTblEntry ** simple_rte_array
Definition: relation.h:202
List * append_rel_list
Definition: relation.h:266
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:422
#define lfirst(lc)
Definition: pg_list.h:106
bool consider_parallel
Definition: relation.h:620
List * partitioned_child_rels
Definition: relation.h:692
PartitionScheme part_scheme
Definition: relation.h:684
Index child_relid
Definition: relation.h:2125
Index parent_relid
Definition: relation.h:2124
Definition: pg_list.h:45

◆ set_append_rel_size()

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

Definition at line 868 of file allpaths.c.

References add_child_rel_equivalences(), adjust_appendrel_attrs(), PlannerInfo::append_rel_list, Assert, RelOptInfo::attr_needed, RelOptInfo::attr_widths, RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictinfo, bms_is_member(), check_stack_depth(), AppendRelInfo::child_relid, RestrictInfo::clause, RelOptInfo::consider_parallel, contain_vars_of_level(), contain_volatile_functions(), DatumGetBool, enable_partition_pruning, eval_const_expressions(), PathTarget::exprs, exprType(), exprTypmod(), find_base_rel(), forboth, get_typavgwidth(), PlannerInfo::glob, RelOptInfo::has_eclass_joins, has_useful_pathkeys(), PlannerInfo::hasPseudoConstantQuals, i, IS_DUMMY_REL, RestrictInfo::is_pushed_down, IS_SIMPLE_REL, IsA, RelOptInfo::joininfo, lappend(), lfirst, list_make1_int, list_nth_node, make_ands_implicit(), make_restrictinfo(), RelOptInfo::max_attr, Min, RelOptInfo::min_attr, NIL, RestrictInfo::outerjoin_delayed, palloc0(), PlannerGlobal::parallelModeOK, AppendRelInfo::parent_relid, RelOptInfo::part_scheme, RelOptInfo::partitioned_child_rels, pfree(), prune_append_rel_partitions(), relation_excluded_by_constraints(), RelOptInfo::relid, RangeTblEntry::relkind, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, rint(), RelOptInfo::rows, RestrictInfo::security_level, RangeTblEntry::securityQuals, set_dummy_rel_pathlist(), set_rel_consider_parallel(), set_rel_size(), PlannerInfo::simple_rte_array, AppendRelInfo::translated_vars, RelOptInfo::tuples, Var::varattno, and PathTarget::width.

Referenced by set_rel_size().

870 {
871  int parentRTindex = rti;
872  bool has_live_children;
873  double parent_rows;
874  double parent_size;
875  double *parent_attrsizes;
876  int nattrs;
877  ListCell *l;
878  Relids live_children = NULL;
879  bool did_pruning = false;
880 
881  /* Guard against stack overflow due to overly deep inheritance tree. */
883 
884  Assert(IS_SIMPLE_REL(rel));
885 
886  /*
887  * Initialize partitioned_child_rels to contain this RT index.
888  *
889  * Note that during the set_append_rel_pathlist() phase, we will bubble up
890  * the indexes of partitioned relations that appear down in the tree, so
891  * that when we've created Paths for all the children, the root
892  * partitioned table's list will contain all such indexes.
893  */
894  if (rte->relkind == RELKIND_PARTITIONED_TABLE)
896 
897  /*
898  * If the partitioned relation has any baserestrictinfo quals then we
899  * attempt to use these quals to prune away partitions that cannot
900  * possibly contain any tuples matching these quals. In this case we'll
901  * store the relids of all partitions which could possibly contain a
902  * matching tuple, and skip anything else in the loop below.
903  */
905  rte->relkind == RELKIND_PARTITIONED_TABLE &&
906  rel->baserestrictinfo != NIL)
907  {
908  live_children = prune_append_rel_partitions(rel);
909  did_pruning = true;
910  }
911 
912  /*
913  * Initialize to compute size estimates for whole append relation.
914  *
915  * We handle width estimates by weighting the widths of different child
916  * rels proportionally to their number of rows. This is sensible because
917  * the use of width estimates is mainly to compute the total relation
918  * "footprint" if we have to sort or hash it. To do this, we sum the
919  * total equivalent size (in "double" arithmetic) and then divide by the
920  * total rowcount estimate. This is done separately for the total rel
921  * width and each attribute.
922  *
923  * Note: if you consider changing this logic, beware that child rels could
924  * have zero rows and/or width, if they were excluded by constraints.
925  */
926  has_live_children = false;
927  parent_rows = 0;
928  parent_size = 0;
929  nattrs = rel->max_attr - rel->min_attr + 1;
930  parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
931 
932  foreach(l, root->append_rel_list)
933  {
934  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
935  int childRTindex;
936  RangeTblEntry *childRTE;
937  RelOptInfo *childrel;
938  List *childquals;
939  Index cq_min_security;
940  bool have_const_false_cq;
941  ListCell *parentvars;
942  ListCell *childvars;
943  ListCell *lc;
944 
945  /* append_rel_list contains all append rels; ignore others */
946  if (appinfo->parent_relid != parentRTindex)
947  continue;
948 
949  childRTindex = appinfo->child_relid;
950  childRTE = root->simple_rte_array[childRTindex];
951 
952  /*
953  * The child rel's RelOptInfo was already created during
954  * add_base_rels_to_query.
955  */
956  childrel = find_base_rel(root, childRTindex);
958 
959  if (rel->part_scheme)
960  {
961  AttrNumber attno;
962 
963  /*
964  * We need attr_needed data for building targetlist of a join
965  * relation representing join between matching partitions for
966  * partitionwise join. A given attribute of a child will be needed
967  * in the same highest joinrel where the corresponding attribute
968  * of parent is needed. Hence it suffices to use the same Relids
969  * set for parent and child.
970  */
971  for (attno = rel->min_attr; attno <= rel->max_attr; attno++)
972  {
973  int index = attno - rel->min_attr;
974  Relids attr_needed = rel->attr_needed[index];
975 
976  /* System attributes do not need translation. */
977  if (attno <= 0)
978  {
979  Assert(rel->min_attr == childrel->min_attr);
980  childrel->attr_needed[index] = attr_needed;
981  }
982  else
983  {
984  Var *var = list_nth_node(Var,
985  appinfo->translated_vars,
986  attno - 1);
987  int child_index;
988 
989  /*
990  * Ignore any column dropped from the parent.
991  * Corresponding Var won't have any translation. It won't
992  * have attr_needed information, since it can not be
993  * referenced in the query.
994  */
995  if (var == NULL)
996  {
997  Assert(attr_needed == NULL);
998  continue;
999  }
1000 
1001  child_index = var->varattno - childrel->min_attr;
1002  childrel->attr_needed[child_index] = attr_needed;
1003  }
1004  }
1005  }
1006 
1007  /*
1008  * Copy/Modify targetlist. Even if this child is deemed empty, we need
1009  * its targetlist in case it falls on nullable side in a child-join
1010  * because of partitionwise join.
1011  *
1012  * NB: the resulting childrel->reltarget->exprs may contain arbitrary
1013  * expressions, which otherwise would not occur in a rel's targetlist.
1014  * Code that might be looking at an appendrel child must cope with
1015  * such. (Normally, a rel's targetlist would only include Vars and
1016  * PlaceHolderVars.) XXX we do not bother to update the cost or width
1017  * fields of childrel->reltarget; not clear if that would be useful.
1018  */
1019  childrel->reltarget->exprs = (List *)
1021  (Node *) rel->reltarget->exprs,
1022  1, &appinfo);
1023 
1024  /*
1025  * We have to make child entries in the EquivalenceClass data
1026  * structures as well. This is needed either if the parent
1027  * participates in some eclass joins (because we will want to consider
1028  * inner-indexscan joins on the individual children) or if the parent
1029  * has useful pathkeys (because we should try to build MergeAppend
1030  * paths that produce those sort orderings). Even if this child is
1031  * deemed dummy, it may fall on nullable side in a child-join, which
1032  * in turn may participate in a MergeAppend, where we will need the
1033  * EquivalenceClass data structures.
1034  */
1035  if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1036  add_child_rel_equivalences(root, appinfo, rel, childrel);
1037  childrel->has_eclass_joins = rel->has_eclass_joins;
1038 
1039  /*
1040  * We have to copy the parent's quals to the child, with appropriate
1041  * substitution of variables. However, only the baserestrictinfo
1042  * quals are needed before we can check for constraint exclusion; so
1043  * do that first and then check to see if we can disregard this child.
1044  *
1045  * The child rel's targetlist might contain non-Var expressions, which
1046  * means that substitution into the quals could produce opportunities
1047  * for const-simplification, and perhaps even pseudoconstant quals.
1048  * Therefore, transform each RestrictInfo separately to see if it
1049  * reduces to a constant or pseudoconstant. (We must process them
1050  * separately to keep track of the security level of each qual.)
1051  */
1052  childquals = NIL;
1053  cq_min_security = UINT_MAX;
1054  have_const_false_cq = false;
1055  foreach(lc, rel->baserestrictinfo)
1056  {
1057  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1058  Node *childqual;
1059  ListCell *lc2;
1060 
1061  Assert(IsA(rinfo, RestrictInfo));
1062  childqual = adjust_appendrel_attrs(root,
1063  (Node *) rinfo->clause,
1064  1, &appinfo);
1065  childqual = eval_const_expressions(root, childqual);
1066  /* check for flat-out constant */
1067  if (childqual && IsA(childqual, Const))
1068  {
1069  if (((Const *) childqual)->constisnull ||
1070  !DatumGetBool(((Const *) childqual)->constvalue))
1071  {
1072  /* Restriction reduces to constant FALSE or NULL */
1073  have_const_false_cq = true;
1074  break;
1075  }
1076  /* Restriction reduces to constant TRUE, so drop it */
1077  continue;
1078  }
1079  /* might have gotten an AND clause, if so flatten it */
1080  foreach(lc2, make_ands_implicit((Expr *) childqual))
1081  {
1082  Node *onecq = (Node *) lfirst(lc2);
1083  bool pseudoconstant;
1084 
1085  /* check for pseudoconstant (no Vars or volatile functions) */
1086  pseudoconstant =
1087  !contain_vars_of_level(onecq, 0) &&
1089  if (pseudoconstant)
1090  {
1091  /* tell createplan.c to check for gating quals */
1092  root->hasPseudoConstantQuals = true;
1093  }
1094  /* reconstitute RestrictInfo with appropriate properties */
1095  childquals = lappend(childquals,
1096  make_restrictinfo((Expr *) onecq,
1097  rinfo->is_pushed_down,
1098  rinfo->outerjoin_delayed,
1099  pseudoconstant,
1100  rinfo->security_level,
1101  NULL, NULL, NULL));
1102  /* track minimum security level among child quals */
1103  cq_min_security = Min(cq_min_security, rinfo->security_level);
1104  }
1105  }
1106 
1107  /*
1108  * In addition to the quals inherited from the parent, we might have
1109  * securityQuals associated with this particular child node.
1110  * (Currently this can only happen in appendrels originating from
1111  * UNION ALL; inheritance child tables don't have their own
1112  * securityQuals, see expand_inherited_rtentry().) Pull any such
1113  * securityQuals up into the baserestrictinfo for the child. This is
1114  * similar to process_security_barrier_quals() for the parent rel,
1115  * except that we can't make any general deductions from such quals,
1116  * since they don't hold for the whole appendrel.
1117  */
1118  if (childRTE->securityQuals)
1119  {
1120  Index security_level = 0;
1121 
1122  foreach(lc, childRTE->securityQuals)
1123  {
1124  List *qualset = (List *) lfirst(lc);
1125  ListCell *lc2;
1126 
1127  foreach(lc2, qualset)
1128  {
1129  Expr *qual = (Expr *) lfirst(lc2);
1130 
1131  /* not likely that we'd see constants here, so no check */
1132  childquals = lappend(childquals,
1133  make_restrictinfo(qual,
1134  true, false, false,
1135  security_level,
1136  NULL, NULL, NULL));
1137  cq_min_security = Min(cq_min_security, security_level);
1138  }
1139  security_level++;
1140  }
1141  Assert(security_level <= root->qual_security_level);
1142  }
1143 
1144  /*
1145  * OK, we've got all the baserestrictinfo quals for this child.
1146  */
1147  childrel->baserestrictinfo = childquals;
1148  childrel->baserestrict_min_security = cq_min_security;
1149 
1150  if (have_const_false_cq)
1151  {
1152  /*
1153  * Some restriction clause reduced to constant FALSE or NULL after
1154  * substitution, so this child need not be scanned.
1155  */
1156  set_dummy_rel_pathlist(childrel);
1157  continue;
1158  }
1159 
1160  if (did_pruning && !bms_is_member(appinfo->child_relid, live_children))
1161  {
1162  /* This partition was pruned; skip it. */
1163  set_dummy_rel_pathlist(childrel);
1164  continue;
1165  }
1166 
1167  if (relation_excluded_by_constraints(root, childrel, childRTE))
1168  {
1169  /*
1170  * This child need not be scanned, so we can omit it from the
1171  * appendrel.
1172  */
1173  set_dummy_rel_pathlist(childrel);
1174  continue;
1175  }
1176 
1177  /* CE failed, so finish copying/modifying join quals. */
1178  childrel->joininfo = (List *)
1180  (Node *) rel->joininfo,
1181  1, &appinfo);
1182 
1183  /*
1184  * If parallelism is allowable for this query in general, see whether
1185  * it's allowable for this childrel in particular. But if we've
1186  * already decided the appendrel is not parallel-safe as a whole,
1187  * there's no point in considering parallelism for this child. For
1188  * consistency, do this before calling set_rel_size() for the child.
1189  */
1190  if (root->glob->parallelModeOK && rel->consider_parallel)
1191  set_rel_consider_parallel(root, childrel, childRTE);
1192 
1193  /*
1194  * Compute the child's size.
1195  */
1196  set_rel_size(root, childrel, childRTindex, childRTE);
1197 
1198  /*
1199  * It is possible that constraint exclusion detected a contradiction
1200  * within a child subquery, even though we didn't prove one above. If
1201  * so, we can skip this child.
1202  */
1203  if (IS_DUMMY_REL(childrel))
1204  continue;
1205 
1206  /* We have at least one live child. */
1207  has_live_children = true;
1208 
1209  /*
1210  * If any live child is not parallel-safe, treat the whole appendrel
1211  * as not parallel-safe. In future we might be able to generate plans
1212  * in which some children are farmed out to workers while others are
1213  * not; but we don't have that today, so it's a waste to consider
1214  * partial paths anywhere in the appendrel unless it's all safe.
1215  * (Child rels visited before this one will be unmarked in
1216  * set_append_rel_pathlist().)
1217  */
1218  if (!childrel->consider_parallel)
1219  rel->consider_parallel = false;
1220 
1221  /*
1222  * Accumulate size information from each live child.
1223  */
1224  Assert(childrel->rows > 0);
1225 
1226  parent_rows += childrel->rows;
1227  parent_size += childrel->reltarget->width * childrel->rows;
1228 
1229  /*
1230  * Accumulate per-column estimates too. We need not do anything for
1231  * PlaceHolderVars in the parent list. If child expression isn't a
1232  * Var, or we didn't record a width estimate for it, we have to fall
1233  * back on a datatype-based estimate.
1234  *
1235  * By construction, child's targetlist is 1-to-1 with parent's.
1236  */
1237  forboth(parentvars, rel->reltarget->exprs,
1238  childvars, childrel->reltarget->exprs)
1239  {
1240  Var *parentvar = (Var *) lfirst(parentvars);
1241  Node *childvar = (Node *) lfirst(childvars);
1242 
1243  if (IsA(parentvar, Var))
1244  {
1245  int pndx = parentvar->varattno - rel->min_attr;
1246  int32 child_width = 0;
1247 
1248  if (IsA(childvar, Var) &&
1249  ((Var *) childvar)->varno == childrel->relid)
1250  {
1251  int cndx = ((Var *) childvar)->varattno - childrel->min_attr;
1252 
1253  child_width = childrel->attr_widths[cndx];
1254  }
1255  if (child_width <= 0)
1256  child_width = get_typavgwidth(exprType(childvar),
1257  exprTypmod(childvar));
1258  Assert(child_width > 0);
1259  parent_attrsizes[pndx] += child_width * childrel->rows;
1260  }
1261  }
1262  }
1263 
1264  if (has_live_children)
1265  {
1266  /*
1267  * Save the finished size estimates.
1268  */
1269  int i;
1270 
1271  Assert(parent_rows > 0);
1272  rel->rows = parent_rows;
1273  rel->reltarget->width = rint(parent_size / parent_rows);
1274  for (i = 0; i < nattrs; i++)
1275  rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1276 
1277  /*
1278  * Set "raw tuples" count equal to "rows" for the appendrel; needed
1279  * because some places assume rel->tuples is valid for any baserel.
1280  */
1281  rel->tuples = parent_rows;
1282  }
1283  else
1284  {
1285  /*
1286  * All children were excluded by constraints, so mark the whole
1287  * appendrel dummy. We must do this in this phase so that the rel's
1288  * dummy-ness is visible when we generate paths for other rels.
1289  */
1291  }
1292 
1293  pfree(parent_attrsizes);
1294 }
bool has_eclass_joins
Definition: relation.h:678
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
Index security_level
Definition: relation.h:1892
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
RelOptKind reloptkind
Definition: relation.h:609
RestrictInfo * make_restrictinfo(Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:57
Relids * attr_needed
Definition: relation.h:645
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
List * securityQuals
Definition: parsenodes.h:1075
double tuples
Definition: relation.h:652
List * baserestrictinfo
Definition: relation.h:672
#define Min(x, y)
Definition: c.h:857
bool relation_excluded_by_constraints(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: plancat.c:1387
Definition: nodes.h:516
AttrNumber varattno
Definition: primnodes.h:168
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2460
Index baserestrict_min_security
Definition: relation.h:674
void add_child_rel_equivalences(PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
Definition: equivclass.c:2109
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
Definition: primnodes.h:163
List * translated_vars
Definition: relation.h:2152
#define IS_SIMPLE_REL(rel)
Definition: relation.h:585
signed int int32
Definition: c.h:313
Definition: type.h:89
void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:2022
void pfree(void *pointer)
Definition: mcxt.c:1031
List * make_ands_implicit(Expr *clause)
Definition: clauses.c:379
Relids prune_append_rel_partitions(RelOptInfo *rel)
Definition: partprune.c:414
#define IS_DUMMY_REL(r)
Definition: relation.h:1317
bool parallelModeOK
Definition: relation.h:143
bool outerjoin_delayed
Definition: relation.h:1884
#define list_nth_node(type, list, n)
Definition: pg_list.h:227
static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:537
List * joininfo
Definition: relation.h:676
void check_stack_depth(void)
Definition: postgres.c:3159
PlannerGlobal * glob
Definition: relation.h:171
#define DatumGetBool(X)
Definition: postgres.h:378
#define list_make1_int(x1)
Definition: pg_list.h:145
double rint(double x)
Definition: rint.c:22
Index relid
Definition: relation.h:640
List * lappend(List *list, void *datum)
Definition: list.c:128
RangeTblEntry ** simple_rte_array
Definition: relation.h:202
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: prepunion.c:2047
Expr * clause
Definition: relation.h:1880
List * exprs
Definition: relation.h:1008
void * palloc0(Size size)
Definition: mcxt.c:955
List * append_rel_list
Definition: relation.h:266
unsigned int Index
Definition: c.h:442
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2332
double rows
Definition: relation.h:615
bool hasPseudoConstantQuals
Definition: relation.h:319
bool is_pushed_down
Definition: relation.h:1882
bool enable_partition_pruning
Definition: costsize.c:141
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool consider_parallel
Definition: relation.h:620
List * partitioned_child_rels
Definition: relation.h:692
AttrNumber max_attr
Definition: relation.h:644
int i
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1659
PartitionScheme part_scheme
Definition: relation.h:684
Index child_relid
Definition: relation.h:2125
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:279
Index parent_relid
Definition: relation.h:2124
int32 * attr_widths
Definition: relation.h:646
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
struct PathTarget * reltarget
Definition: relation.h:623
int16 AttrNumber
Definition: attnum.h:21
static void set_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:319
AttrNumber min_attr
Definition: relation.h:643

◆ set_base_rel_consider_startup()

static void set_base_rel_consider_startup ( PlannerInfo root)
static

Definition at line 206 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().

207 {
208  /*
209  * Since parameterized paths can only be used on the inside of a nestloop
210  * join plan, there is usually little value in considering fast-start
211  * plans for them. However, for relations that are on the RHS of a SEMI
212  * or ANTI join, a fast-start plan can be useful because we're only going
213  * to care about fetching one tuple anyway.
214  *
215  * To minimize growth of planning time, we currently restrict this to
216  * cases where the RHS is a single base relation, not a join; there is no
217  * provision for consider_param_startup to get set at all on joinrels.
218  * Also we don't worry about appendrels. costsize.c's costing rules for
219  * nestloop semi/antijoins don't consider such cases either.
220  */
221  ListCell *lc;
222 
223  foreach(lc, root->join_info_list)
224  {
225  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
226  int varno;
227 
228  if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
229  bms_get_singleton_member(sjinfo->syn_righthand, &varno))
230  {
231  RelOptInfo *rel = find_base_rel(root, varno);
232 
233  rel->consider_param_startup = true;
234  }
235  }
236 }
List * join_info_list
Definition: relation.h:264
bool consider_param_startup
Definition: relation.h:619
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:635
Relids syn_righthand
Definition: relation.h:2069
#define lfirst(lc)
Definition: pg_list.h:106
JoinType jointype
Definition: relation.h:2070
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:279

◆ set_base_rel_pathlists()

static void set_base_rel_pathlists ( PlannerInfo root)
static

Definition at line 292 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().

293 {
294  Index rti;
295 
296  for (rti = 1; rti < root->simple_rel_array_size; rti++)
297  {
298  RelOptInfo *rel = root->simple_rel_array[rti];
299 
300  /* there may be empty slots corresponding to non-baserel RTEs */
301  if (rel == NULL)
302  continue;
303 
304  Assert(rel->relid == rti); /* sanity check on array */
305 
306  /* ignore RTEs that are "other rels" */
307  if (rel->reloptkind != RELOPT_BASEREL)
308  continue;
309 
310  set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
311  }
312 }
RelOptKind reloptkind
Definition: relation.h:609
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
int simple_rel_array_size
Definition: relation.h:194
Index relid
Definition: relation.h:640
RangeTblEntry ** simple_rte_array
Definition: relation.h:202
unsigned int Index
Definition: c.h:442
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:422
#define Assert(condition)
Definition: c.h:699

◆ set_base_rel_sizes()

static void set_base_rel_sizes ( PlannerInfo root)
static

Definition at line 249 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().

250 {
251  Index rti;
252 
253  for (rti = 1; rti < root->simple_rel_array_size; rti++)
254  {
255  RelOptInfo *rel = root->simple_rel_array[rti];
256  RangeTblEntry *rte;
257 
258  /* there may be empty slots corresponding to non-baserel RTEs */
259  if (rel == NULL)
260  continue;
261 
262  Assert(rel->relid == rti); /* sanity check on array */
263 
264  /* ignore RTEs that are "other rels" */
265  if (rel->reloptkind != RELOPT_BASEREL)
266  continue;
267 
268  rte = root->simple_rte_array[rti];
269 
270  /*
271  * If parallelism is allowable for this query in general, see whether
272  * it's allowable for this rel in particular. We have to do this
273  * before set_rel_size(), because (a) if this rel is an inheritance
274  * parent, set_append_rel_size() will use and perhaps change the rel's
275  * consider_parallel flag, and (b) for some RTE types, set_rel_size()
276  * goes ahead and makes paths immediately.
277  */
278  if (root->glob->parallelModeOK)
279  set_rel_consider_parallel(root, rel, rte);
280 
281  set_rel_size(root, rel, rti, rte);
282  }
283 }
RelOptKind reloptkind
Definition: relation.h:609
struct RelOptInfo ** simple_rel_array
Definition: relation.h:193
bool parallelModeOK
Definition: relation.h:143
static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:537
PlannerGlobal * glob
Definition: relation.h:171
int simple_rel_array_size
Definition: relation.h:194
Index relid
Definition: relation.h:640
RangeTblEntry ** simple_rte_array
Definition: relation.h:202
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699
static void set_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:319

◆ set_cte_pathlist()

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

Definition at line 2395 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().

2396 {
2397  Plan *cteplan;
2398  PlannerInfo *cteroot;
2399  Index levelsup;
2400  int ndx;
2401  ListCell *lc;
2402  int plan_id;
2403  Relids required_outer;
2404 
2405  /*
2406  * Find the referenced CTE, and locate the plan previously made for it.
2407  */
2408  levelsup = rte->ctelevelsup;
2409  cteroot = root;
2410  while (levelsup-- > 0)
2411  {
2412  cteroot = cteroot->parent_root;
2413  if (!cteroot) /* shouldn't happen */
2414  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2415  }
2416 
2417  /*
2418  * Note: cte_plan_ids can be shorter than cteList, if we are still working
2419  * on planning the CTEs (ie, this is a side-reference from another CTE).
2420  * So we mustn't use forboth here.
2421  */
2422  ndx = 0;
2423  foreach(lc, cteroot->parse->cteList)
2424  {
2425  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2426 
2427  if (strcmp(cte->ctename, rte->ctename) == 0)
2428  break;
2429  ndx++;
2430  }
2431  if (lc == NULL) /* shouldn't happen */
2432  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2433  if (ndx >= list_length(cteroot->cte_plan_ids))
2434  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2435  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2436  Assert(plan_id > 0);
2437  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2438 
2439  /* Mark rel with estimated output rows, width, etc */
2440  set_cte_size_estimates(root, rel, cteplan->plan_rows);
2441 
2442  /*
2443  * We don't support pushing join clauses into the quals of a CTE scan, but
2444  * it could still have required parameterization due to LATERAL refs in
2445  * its tlist.
2446  */
2447  required_outer = rel->lateral_relids;
2448 
2449  /* Generate appropriate path */
2450  add_path(rel, create_ctescan_path(root, rel, required_outer));
2451 }
double plan_rows
Definition: plannodes.h:134
Query * parse
Definition: relation.h:169
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:4982
Relids lateral_relids
Definition: relation.h:637
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
List * subplans
Definition: relation.h:112
PlannerGlobal * glob
Definition: relation.h:171
struct PlannerInfo * parent_root
Definition: relation.h:175
int list_nth_int(const List *list, int n)
Definition: list.c:421
List * cte_plan_ids
Definition: relation.h:244
unsigned int Index
Definition: c.h:442
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Index ctelevelsup
Definition: parsenodes.h:1033
List * cteList
Definition: parsenodes.h:135
char * ctename
Definition: parsenodes.h:1032
#define elog
Definition: elog.h:219
Path * create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1977

◆ set_dummy_rel_pathlist()

void set_dummy_rel_pathlist ( RelOptInfo rel)

Definition at line 2022 of file allpaths.c.

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

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

2023 {
2024  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
2025  rel->rows = 0;
2026  rel->reltarget->width = 0;
2027 
2028  /* Discard any pre-existing paths; no further need for them */
2029  rel->pathlist = NIL;
2030  rel->partial_pathlist = NIL;
2031 
2032  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL, NULL,
2033  0, false, NIL, -1));
2034 
2035  /*
2036  * We set the cheapest path immediately, to ensure that IS_DUMMY_REL()
2037  * will recognize the relation as dummy if anyone asks. This is redundant
2038  * when we're called from set_rel_size(), but not when called from
2039  * elsewhere, and doing it twice is harmless anyway.
2040  */
2041  set_cheapest(rel);
2042 }
#define NIL
Definition: pg_list.h:69
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, Relids required_outer, int parallel_workers, bool parallel_aware, List *partitioned_rels, double rows)
Definition: pathnode.c:1219
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
List * partial_pathlist
Definition: relation.h:628
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
double rows
Definition: relation.h:615
List * pathlist
Definition: relation.h:626
struct PathTarget * reltarget
Definition: relation.h:623

◆ set_foreign_pathlist()

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

Definition at line 850 of file allpaths.c.

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

Referenced by set_rel_pathlist().

851 {
852  /* Call the FDW's GetForeignPaths function to generate path(s) */
853  rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
854 }
struct FdwRoutine * fdwroutine
Definition: relation.h:663
GetForeignPaths_function GetForeignPaths
Definition: fdwapi.h:188

◆ set_foreign_size()

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

Definition at line 833 of file allpaths.c.

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

Referenced by set_rel_size().

834 {
835  /* Mark rel with estimated output rows, width, etc */
836  set_foreign_size_estimates(root, rel);
837 
838  /* Let FDW adjust the size estimates, if it can */
839  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
840 
841  /* ... but do not let it set the rows estimate to zero */
842  rel->rows = clamp_row_est(rel->rows);
843 }
struct FdwRoutine * fdwroutine
Definition: relation.h:663
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:187
double rows
Definition: relation.h:615
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5058
double clamp_row_est(double nrows)
Definition: costsize.c:188

◆ set_function_pathlist()

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

Definition at line 2283 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().

2284 {
2285  Relids required_outer;
2286  List *pathkeys = NIL;
2287 
2288  /*
2289  * We don't support pushing join clauses into the quals of a function
2290  * scan, but it could still have required parameterization due to LATERAL
2291  * refs in the function expression.
2292  */
2293  required_outer = rel->lateral_relids;
2294 
2295  /*
2296  * The result is considered unordered unless ORDINALITY was used, in which
2297  * case it is ordered by the ordinal column (the last one). See if we
2298  * care, by checking for uses of that Var in equivalence classes.
2299  */
2300  if (rte->funcordinality)
2301  {
2302  AttrNumber ordattno = rel->max_attr;
2303  Var *var = NULL;
2304  ListCell *lc;
2305 
2306  /*
2307  * Is there a Var for it in rel's targetlist? If not, the query did
2308  * not reference the ordinality column, or at least not in any way
2309  * that would be interesting for sorting.
2310  */
2311  foreach(lc, rel->reltarget->exprs)
2312  {
2313  Var *node = (Var *) lfirst(lc);
2314 
2315  /* checking varno/varlevelsup is just paranoia */
2316  if (IsA(node, Var) &&
2317  node->varattno == ordattno &&
2318  node->varno == rel->relid &&
2319  node->varlevelsup == 0)
2320  {
2321  var = node;
2322  break;
2323  }
2324  }
2325 
2326  /*
2327  * Try to build pathkeys for this Var with int8 sorting. We tell
2328  * build_expression_pathkey not to build any new equivalence class; if
2329  * the Var isn't already mentioned in some EC, it means that nothing
2330  * cares about the ordering.
2331  */
2332  if (var)
2333  pathkeys = build_expression_pathkey(root,
2334  (Expr *) var,
2335  NULL, /* below outer joins */
2336  Int8LessOperator,
2337  rel->relids,
2338  false);
2339  }
2340 
2341  /* Generate appropriate path */
2342  add_path(rel, create_functionscan_path(root, rel,
2343  pathkeys, required_outer));
2344 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid opno, Relids rel, bool create_it)
Definition: pathkeys.c:562
Index varlevelsup
Definition: primnodes.h:173
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
AttrNumber varattno
Definition: primnodes.h:168
bool funcordinality
Definition: parsenodes.h:1017
Definition: primnodes.h:163
Path * create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1899
Relids lateral_relids
Definition: relation.h:637
Relids relids
Definition: relation.h:612
Index relid
Definition: relation.h:640
Index varno
Definition: primnodes.h:166
List * exprs
Definition: relation.h:1008
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber max_attr
Definition: relation.h:644
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:623
int16 AttrNumber
Definition: attnum.h:21

◆ set_namedtuplestore_pathlist()

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

Definition at line 2461 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().

2463 {
2464  Relids required_outer;
2465 
2466  /* Mark rel with estimated output rows, width, etc */
2468 
2469  /*
2470  * We don't support pushing join clauses into the quals of a tuplestore
2471  * scan, but it could still have required parameterization due to LATERAL
2472  * refs in its tlist.
2473  */
2474  required_outer = rel->lateral_relids;
2475 
2476  /* Generate appropriate path */
2477  add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
2478 
2479  /* Select cheapest path (pretty easy in this case...) */
2480  set_cheapest(rel);
2481 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
Relids lateral_relids
Definition: relation.h:637
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void set_namedtuplestore_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5019
Path * create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2002

◆ set_plain_rel_pathlist()

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

Definition at line 695 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().

696 {
697  Relids required_outer;
698 
699  /*
700  * We don't support pushing join clauses into the quals of a seqscan, but
701  * it could still have required parameterization due to LATERAL refs in
702  * its tlist.
703  */
704  required_outer = rel->lateral_relids;
705 
706  /* Consider sequential scan */
707  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
708 
709  /* If appropriate, consider parallel sequential scan */
710  if (rel->consider_parallel && required_outer == NULL)
711  create_plain_partial_paths(root, rel);
712 
713  /* Consider index scans */
714  create_index_paths(root, rel);
715 
716  /* Consider TID scans */
717  create_tidscan_paths(root, rel);
718 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
void create_index_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:230
Relids lateral_relids
Definition: relation.h:637
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:253
bool consider_parallel
Definition: relation.h:620
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:725
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:954

◆ set_plain_rel_size()

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

Definition at line 520 of file allpaths.c.

References check_index_predicates(), and set_baserel_size_estimates().

Referenced by set_rel_size().

521 {
522  /*
523  * Test any partial indexes of rel for applicability. We must do this
524  * first since partial unique indexes can affect size estimates.
525  */
526  check_index_predicates(root, rel);
527 
528  /* Mark rel with estimated output rows, width, etc */
529  set_baserel_size_estimates(root, rel);
530 }
void check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:2795
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4295

◆ set_rel_consider_parallel()

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

Definition at line 537 of file allpaths.c.

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

Referenced by set_append_rel_size(), and set_base_rel_sizes().

539 {
540  /*
541  * The flag has previously been initialized to false, so we can just
542  * return if it becomes clear that we can't safely set it.
543  */
544  Assert(!rel->consider_parallel);
545 
546  /* Don't call this if parallelism is disallowed for the entire query. */
547  Assert(root->glob->parallelModeOK);
548 
549  /* This should only be called for baserels and appendrel children. */
550  Assert(IS_SIMPLE_REL(rel));
551 
552  /* Assorted checks based on rtekind. */
553  switch (rte->rtekind)
554  {
555  case RTE_RELATION:
556 
557  /*
558  * Currently, parallel workers can't access the leader's temporary
559  * tables. We could possibly relax this if the wrote all of its
560  * local buffers at the start of the query and made no changes
561  * thereafter (maybe we could allow hint bit changes), and if we
562  * taught the workers to read them. Writing a large number of
563  * temporary buffers could be expensive, though, and we don't have
564  * the rest of the necessary infrastructure right now anyway. So
565  * for now, bail out if we see a temporary table.
566  */
567  if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
568  return;
569 
570  /*
571  * Table sampling can be pushed down to workers if the sample
572  * function and its arguments are safe.
573  */
574  if (rte->tablesample != NULL)
575  {
576  char proparallel = func_parallel(rte->tablesample->tsmhandler);
577 
578  if (proparallel != PROPARALLEL_SAFE)
579  return;
580  if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
581  return;
582  }
583 
584  /*
585  * Ask FDWs whether they can support performing a ForeignScan
586  * within a worker. Most often, the answer will be no. For
587  * example, if the nature of the FDW is such that it opens a TCP
588  * connection with a remote server, each parallel worker would end
589  * up with a separate connection, and these connections might not
590  * be appropriately coordinated between workers and the leader.
591  */
592  if (rte->relkind == RELKIND_FOREIGN_TABLE)
593  {
594  Assert(rel->fdwroutine);
596  return;
597  if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
598  return;
599  }
600 
601  /*
602  * There are additional considerations for appendrels, which we'll
603  * deal with in set_append_rel_size and set_append_rel_pathlist.
604  * For now, just set consider_parallel based on the rel's own
605  * quals and targetlist.
606  */
607  break;
608 
609  case RTE_SUBQUERY:
610 
611  /*
612  * There's no intrinsic problem with scanning a subquery-in-FROM
613  * (as distinct from a SubPlan or InitPlan) in a parallel worker.
614  * If the subquery doesn't happen to have any parallel-safe paths,
615  * then flagging it as consider_parallel won't change anything,
616  * but that's true for plain tables, too. We must set
617  * consider_parallel based on the rel's own quals and targetlist,
618  * so that if a subquery path is parallel-safe but the quals and
619  * projection we're sticking onto it are not, we correctly mark
620  * the SubqueryScanPath as not parallel-safe. (Note that
621  * set_subquery_pathlist() might push some of these quals down
622  * into the subquery itself, but that doesn't change anything.)
623  */
624  break;
625 
626  case RTE_JOIN:
627  /* Shouldn't happen; we're only considering baserels here. */
628  Assert(false);
629  return;
630 
631  case RTE_FUNCTION:
632  /* Check for parallel-restricted functions. */
633  if (!is_parallel_safe(root, (Node *) rte->functions))
634  return;
635  break;
636 
637  case RTE_TABLEFUNC:
638  /* not parallel safe */
639  return;
640 
641  case RTE_VALUES:
642  /* Check for parallel-restricted functions. */
643  if (!is_parallel_safe(root, (Node *) rte->values_lists))
644  return;
645  break;
646 
647  case RTE_CTE:
648 
649  /*
650  * CTE tuplestores aren't shared among parallel workers, so we
651  * force all CTE scans to happen in the leader. Also, populating
652  * the CTE would require executing a subplan that's not available
653  * in the worker, might be parallel-restricted, and must get
654  * executed only once.
655  */
656  return;
657 
658  case RTE_NAMEDTUPLESTORE:
659 
660  /*
661  * tuplestore cannot be shared, at least without more
662  * infrastructure to support that.
663  */
664  return;
665  }
666 
667  /*
668  * If there's anything in baserestrictinfo that's parallel-restricted, we
669  * give up on parallelizing access to this relation. We could consider
670  * instead postponing application of the restricted quals until we're
671  * above all the parallelism in the plan tree, but it's not clear that
672  * that would be a win in very many cases, and it might be tricky to make
673  * outer join clauses work correctly. It would likely break equivalence
674  * classes, too.
675  */
676  if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
677  return;
678 
679  /*
680  * Likewise, if the relation's outputs are not parallel-safe, give up.
681  * (Usually, they're just Vars, but sometimes they're not.)
682  */
683  if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
684  return;
685 
686  /* We have a winner. */
687  rel->consider_parallel = true;
688 }
List * baserestrictinfo
Definition: relation.h:672
Definition: nodes.h:516
List * values_lists
Definition: parsenodes.h:1027
#define IS_SIMPLE_REL(rel)
Definition: relation.h:585
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1088
IsForeignScanParallelSafe_function IsForeignScanParallelSafe
Definition: fdwapi.h:239
bool parallelModeOK
Definition: relation.h:143
PlannerGlobal * glob
Definition: relation.h:171
struct FdwRoutine * fdwroutine
Definition: relation.h:663
List * exprs
Definition: relation.h:1008
#define Assert(condition)
Definition: c.h:699
List * functions
Definition: parsenodes.h:1016
char func_parallel(Oid funcid)
Definition: lsyscache.c:1588
bool consider_parallel
Definition: relation.h:620
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:1856
RTEKind rtekind
Definition: parsenodes.h:962
struct PathTarget * reltarget
Definition: relation.h:623
struct TableSampleClause * tablesample
Definition: parsenodes.h:980

◆ set_rel_pathlist()

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

Definition at line 422 of file allpaths.c.

References PlannerInfo::all_baserels, bms_membership(), BMS_SINGLETON, elog, ERROR, generate_gather_paths(), RangeTblEntry::inh, IS_DUMMY_REL, RangeTblEntry::relkind, RELOPT_BASEREL, RelOptInfo::reloptkind, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, 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().

424 {
425  if (IS_DUMMY_REL(rel))
426  {
427  /* We already proved the relation empty, so nothing more to do */
428  }
429  else if (rte->inh)
430  {
431  /* It's an "append relation", process accordingly */
432  set_append_rel_pathlist(root, rel, rti, rte);
433  }
434  else
435  {
436  switch (rel->rtekind)
437  {
438  case RTE_RELATION:
439  if (rte->relkind == RELKIND_FOREIGN_TABLE)
440  {
441  /* Foreign table */
442  set_foreign_pathlist(root, rel, rte);
443  }
444  else if (rte->tablesample != NULL)
445  {
446  /* Sampled relation */
447  set_tablesample_rel_pathlist(root, rel, rte);
448  }
449  else
450  {
451  /* Plain relation */
452  set_plain_rel_pathlist(root, rel, rte);
453  }
454  break;
455  case RTE_SUBQUERY:
456  /* Subquery --- fully handled during set_rel_size */
457  break;
458  case RTE_FUNCTION:
459  /* RangeFunction */
460  set_function_pathlist(root, rel, rte);
461  break;
462  case RTE_TABLEFUNC:
463  /* Table Function */
464  set_tablefunc_pathlist(root, rel, rte);
465  break;
466  case RTE_VALUES:
467  /* Values list */
468  set_values_pathlist(root, rel, rte);
469  break;
470  case RTE_CTE:
471  /* CTE reference --- fully handled during set_rel_size */
472  break;
473  case RTE_NAMEDTUPLESTORE:
474  /* tuplestore reference --- fully handled during set_rel_size */
475  break;
476  default:
477  elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
478  break;
479  }
480  }
481 
482  /*
483  * If this is a baserel, we should normally consider gathering any partial
484  * paths we may have created for it.
485  *
486  * However, if this is an inheritance child, skip it. Otherwise, we could
487  * end up with a very large number of gather nodes, each trying to grab
488  * its own pool of workers. Instead, we'll consider gathering partial
489  * paths for the parent appendrel.
490  *
491  * Also, if this is the topmost scan/join rel (that is, the only baserel),
492  * we postpone this until the final scan/join targelist is available (see
493  * grouping_planner).
494  */
495  if (rel->reloptkind == RELOPT_BASEREL &&
497  generate_gather_paths(root, rel, false);
498 
499  /*
500  * Allow a plugin to editorialize on the set of Paths for this base
501  * relation. It could add new paths (such as CustomPaths) by calling
502  * add_path(), or delete or modify paths added by the core code.
503  */
505  (*set_rel_pathlist_hook) (root, rel, rti, rte);
506 
507  /* Now find the cheapest of the paths for this rel */
508  set_cheapest(rel);
509 
510 #ifdef OPTIMIZER_DEBUG
511  debug_print_rel(root, rel);
512 #endif
513 }
RelOptKind reloptkind
Definition: relation.h:609
set_rel_pathlist_hook_type set_rel_pathlist_hook
Definition: allpaths.c:66
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:2550
Relids all_baserels
Definition: relation.h:210
#define ERROR
Definition: elog.h:43
#define IS_DUMMY_REL(r)
Definition: relation.h:1317
static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:1301
static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:850
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:695
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:700
RTEKind rtekind
Definition: relation.h:642
static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:785
static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2371
#define elog
Definition: elog.h:219
static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2283
static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2351
struct TableSampleClause * tablesample
Definition: parsenodes.h:980

◆ set_rel_size()

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

Definition at line 319 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_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_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().

321 {
322  if (rel->reloptkind == RELOPT_BASEREL &&
323  relation_excluded_by_constraints(root, rel, rte))
324  {
325  /*
326  * We proved we don't need to scan the rel via constraint exclusion,
327  * so set up a single dummy path for it. Here we only check this for
328  * regular baserels; if it's an otherrel, CE was already checked in
329  * set_append_rel_size().
330  *
331  * In this case, we go ahead and set up the relation's path right away
332  * instead of leaving it for set_rel_pathlist to do. This is because
333  * we don't have a convention for marking a rel as dummy except by
334  * assigning a dummy path to it.
335  */
337  }
338  else if (rte->inh)
339  {
340  /* It's an "append relation", process accordingly */
341  set_append_rel_size(root, rel, rti, rte);
342  }
343  else
344  {
345  switch (rel->rtekind)
346  {
347  case RTE_RELATION:
348  if (rte->relkind == RELKIND_FOREIGN_TABLE)
349  {
350  /* Foreign table */
351  set_foreign_size(root, rel, rte);
352  }
353  else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
354  {
355  /*
356  * A partitioned table without any partitions is marked as
357  * a dummy rel.
358  */
360  }
361  else if (rte->tablesample != NULL)
362  {
363  /* Sampled relation */
364  set_tablesample_rel_size(root, rel, rte);
365  }
366  else
367  {
368  /* Plain relation */
369  set_plain_rel_size(root, rel, rte);
370  }
371  break;
372  case RTE_SUBQUERY:
373 
374  /*
375  * Subqueries don't support making a choice between
376  * parameterized and unparameterized paths, so just go ahead
377  * and build their paths immediately.
378  */
379  set_subquery_pathlist(root, rel, rti, rte);
380  break;
381  case RTE_FUNCTION:
382  set_function_size_estimates(root, rel);
383  break;
384  case RTE_TABLEFUNC:
385  set_tablefunc_size_estimates(root, rel);
386  break;
387  case RTE_VALUES:
388  set_values_size_estimates(root, rel);
389  break;
390  case RTE_CTE:
391 
392  /*
393  * CTEs don't support making a choice between parameterized
394  * and unparameterized paths, so just go ahead and build their
395  * paths immediately.
396  */
397  if (rte->self_reference)
398  set_worktable_pathlist(root, rel, rte);
399  else
400  set_cte_pathlist(root, rel, rte);
401  break;
402  case RTE_NAMEDTUPLESTORE:
403  set_namedtuplestore_pathlist(root, rel, rte);
404  break;
405  default:
406  elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
407  break;
408  }
409  }
410 
411  /*
412  * We insist that all non-dummy rels have a nonzero rowcount estimate.
413  */
414  Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
415 }
RelOptKind reloptkind
Definition: relation.h:609
static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2395
static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:833
bool relation_excluded_by_constraints(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: plancat.c:1387
static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2461
static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:868
void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:2022
#define ERROR
Definition: elog.h:43
#define IS_DUMMY_REL(r)
Definition: relation.h:1317
static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:2079
static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:745
bool self_reference
Definition: parsenodes.h:1034
RTEKind rtekind
Definition: relation.h:642
double rows
Definition: relation.h:615
#define Assert(condition)
Definition: c.h:699
void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4950
static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:520
void set_tablefunc_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4928
void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4890
#define elog
Definition: elog.h:219
struct TableSampleClause * tablesample
Definition: parsenodes.h:980
static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2491

◆ set_subquery_pathlist()

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

Definition at line 2079 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().

2081 {
2082  Query *parse = root->parse;
2083  Query *subquery = rte->subquery;
2084  Relids required_outer;
2085  pushdown_safety_info safetyInfo;
2086  double tuple_fraction;
2087  RelOptInfo *sub_final_rel;
2088  ListCell *lc;
2089 
2090  /*
2091  * Must copy the Query so that planning doesn't mess up the RTE contents
2092  * (really really need to fix the planner to not scribble on its input,
2093  * someday ... but see remove_unused_subquery_outputs to start with).
2094  */
2095  subquery = copyObject(subquery);
2096 
2097  /*
2098  * If it's a LATERAL subquery, it might contain some Vars of the current
2099  * query level, requiring it to be treated as parameterized, even though
2100  * we don't support pushing down join quals into subqueries.
2101  */
2102  required_outer = rel->lateral_relids;
2103 
2104  /*
2105  * Zero out result area for subquery_is_pushdown_safe, so that it can set
2106  * flags as needed while recursing. In particular, we need a workspace
2107  * for keeping track of unsafe-to-reference columns. unsafeColumns[i]
2108  * will be set true if we find that output column i of the subquery is
2109  * unsafe to use in a pushed-down qual.
2110  */
2111  memset(&safetyInfo, 0, sizeof(safetyInfo));
2112  safetyInfo.unsafeColumns = (bool *)
2113  palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
2114 
2115  /*
2116  * If the subquery has the "security_barrier" flag, it means the subquery
2117  * originated from a view that must enforce row level security. Then we
2118  * must not push down quals that contain leaky functions. (Ideally this
2119  * would be checked inside subquery_is_pushdown_safe, but since we don't
2120  * currently pass the RTE to that function, we must do it here.)
2121  */
2122  safetyInfo.unsafeLeaky = rte->security_barrier;
2123 
2124  /*
2125  * If there are any restriction clauses that have been attached to the
2126  * subquery relation, consider pushing them down to become WHERE or HAVING
2127  * quals of the subquery itself. This transformation is useful because it
2128  * may allow us to generate a better plan for the subquery than evaluating
2129  * all the subquery output rows and then filtering them.
2130  *
2131  * There are several cases where we cannot push down clauses. Restrictions
2132  * involving the subquery are checked by subquery_is_pushdown_safe().
2133  * Restrictions on individual clauses are checked by
2134  * qual_is_pushdown_safe(). Also, we don't want to push down
2135  * pseudoconstant clauses; better to have the gating node above the
2136  * subquery.
2137  *
2138  * Non-pushed-down clauses will get evaluated as qpquals of the
2139  * SubqueryScan node.
2140  *
2141  * XXX Are there any cases where we want to make a policy decision not to
2142  * push down a pushable qual, because it'd result in a worse plan?
2143  */
2144  if (rel->baserestrictinfo != NIL &&
2145  subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
2146  {
2147  /* OK to consider pushing down individual quals */
2148  List *upperrestrictlist = NIL;
2149  ListCell *l;
2150 
2151  foreach(l, rel->baserestrictinfo)
2152  {
2153  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2154  Node *clause = (Node *) rinfo->clause;
2155 
2156  if (!rinfo->pseudoconstant &&
2157  qual_is_pushdown_safe(subquery, rti, clause, &safetyInfo))
2158  {
2159  /* Push it down */
2160  subquery_push_qual(subquery, rte, rti, clause);
2161  }
2162  else
2163  {
2164  /* Keep it in the upper query */
2165  upperrestrictlist = lappend(upperrestrictlist, rinfo);
2166  }
2167  }
2168  rel->baserestrictinfo = upperrestrictlist;
2169  /* We don't bother recomputing baserestrict_min_security */
2170  }
2171 
2172  pfree(safetyInfo.unsafeColumns);
2173 
2174  /*
2175  * The upper query might not use all the subquery's output columns; if
2176  * not, we can simplify.
2177  */
2178  remove_unused_subquery_outputs(subquery, rel);
2179 
2180  /*
2181  * We can safely pass the outer tuple_fraction down to the subquery if the
2182  * outer level has no joining, aggregation, or sorting to do. Otherwise
2183  * we'd better tell the subquery to plan for full retrieval. (XXX This
2184  * could probably be made more intelligent ...)
2185  */
2186  if (parse->hasAggs ||
2187  parse->groupClause ||
2188  parse->groupingSets ||
2189  parse->havingQual ||
2190  parse->distinctClause ||
2191  parse->sortClause ||
2192  has_multiple_baserels(root))
2193  tuple_fraction = 0.0; /* default case */
2194  else
2195  tuple_fraction = root->tuple_fraction;
2196 
2197  /* plan_params should not be in use in current query level */
2198  Assert(root->plan_params == NIL);
2199 
2200  /* Generate a subroot and Paths for the subquery */
2201  rel->subroot = subquery_planner(root->glob, subquery,
2202  root,
2203  false, tuple_fraction);
2204 
2205  /* Isolate the params needed by this specific subplan */
2206  rel->subplan_params = root->plan_params;
2207  root->plan_params = NIL;
2208 
2209  /*
2210  * It's possible that constraint exclusion proved the subquery empty. If
2211  * so, it's desirable to produce an unadorned dummy path so that we will
2212  * recognize appropriate optimizations at this query level.
2213  */
2214  sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
2215 
2216  if (IS_DUMMY_REL(sub_final_rel))
2217  {
2219  return;
2220  }
2221 
2222  /*
2223  * Mark rel with estimated output rows, width, etc. Note that we have to
2224  * do this before generating outer-query paths, else cost_subqueryscan is
2225  * not happy.
2226  */
2227  set_subquery_size_estimates(root, rel);
2228 
2229  /*
2230  * For each Path that subquery_planner produced, make a SubqueryScanPath
2231  * in the outer query.
2232  */
2233  foreach(lc, sub_final_rel->pathlist)
2234  {
2235  Path *subpath = (Path *) lfirst(lc);
2236  List *pathkeys;
2237 
2238  /* Convert subpath's pathkeys to outer representation */
2239  pathkeys = convert_subquery_pathkeys(root,
2240  rel,
2241  subpath->pathkeys,
2243 
2244  /* Generate outer path using this subpath */
2245  add_path(rel, (Path *)
2246  create_subqueryscan_path(root, rel, subpath,
2247  pathkeys, required_outer));
2248  }
2249 
2250  /* If outer rel allows parallelism, do same for partial paths. */
2251  if (rel->consider_parallel && bms_is_empty(required_outer))
2252  {
2253  /* If consider_parallel is false, there should be no partial paths. */
2254  Assert(sub_final_rel->consider_parallel ||
2255  sub_final_rel->partial_pathlist == NIL);
2256 
2257  /* Same for partial paths. */
2258  foreach(lc, sub_final_rel->partial_pathlist)
2259  {
2260  Path *subpath = (Path *) lfirst(lc);
2261  List *pathkeys;
2262 
2263  /* Convert subpath's pathkeys to outer representation */
2264  pathkeys = convert_subquery_pathkeys(root,
2265  rel,
2266  subpath->pathkeys,
2268 
2269  /* Generate outer path using this subpath */
2270  add_partial_path(rel, (Path *)
2271  create_subqueryscan_path(root, rel, subpath,
2272  pathkeys,
2273  required_outer));
2274  }
2275  }
2276 }
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4810
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:1079
Query * parse
Definition: relation.h:169
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:1871
List * plan_params
Definition: relation.h:183
List * sortClause
Definition: parsenodes.h:158
List * baserestrictinfo
Definition: relation.h:672
bool hasAggs
Definition: parsenodes.h:125
bool pseudoconstant
Definition: relation.h:1888
List * groupingSets
Definition: parsenodes.h:150
Definition: nodes.h:516
List * partial_pathlist
Definition: relation.h:628
List * make_tlist_from_pathtarget(PathTarget *target)
Definition: tlist.c:595
bool * unsafeColumns
Definition: allpaths.c:54
char bool
Definition: c.h:275
List * targetList
Definition: parsenodes.h:140
PlannerInfo * subroot
Definition: relation.h:654
Relids lateral_relids
Definition: relation.h:637
double tuple_fraction
Definition: relation.h:306
void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:2022
void pfree(void *pointer)
Definition: mcxt.c:1031
List * distinctClause
Definition: parsenodes.h:156
#define IS_DUMMY_REL(r)
Definition: relation.h:1317
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1145
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
Definition: pathkeys.c:616
PlannerGlobal * glob
Definition: relation.h:171
static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
Definition: allpaths.c:3302
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1880
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3131
void * palloc0(Size size)
Definition: mcxt.c:955
bool security_barrier
Definition: parsenodes.h:986
List * pathkeys
Definition: relation.h:1092
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
static bool has_multiple_baserels(PlannerInfo *root)
Definition: allpaths.c:2046
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3208
static int list_length(const List *l)
Definition: pg_list.h:89
bool consider_parallel
Definition: relation.h:620
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2869
Query * subquery
Definition: parsenodes.h:985
List * groupClause
Definition: parsenodes.h:148
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:762
List * pathlist
Definition: relation.h:626
#define copyObject(obj)
Definition: nodes.h:629
Node * havingQual
Definition: parsenodes.h:152
Definition: pg_list.h:45
List * subplan_params
Definition: relation.h:655
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:594
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649

◆ set_tablefunc_pathlist()

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

Definition at line 2371 of file allpaths.c.

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

Referenced by set_rel_pathlist().

2372 {
2373  Relids required_outer;
2374 
2375  /*
2376  * We don't support pushing join clauses into the quals of a tablefunc
2377  * scan, but it could still have required parameterization due to LATERAL
2378  * refs in the function expression.
2379  */
2380  required_outer = rel->lateral_relids;
2381 
2382  /* Generate appropriate path */
2383  add_path(rel, create_tablefuncscan_path(root, rel,
2384  required_outer));
2385 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
Relids lateral_relids
Definition: relation.h:637
Path * create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1925

◆ set_tablesample_rel_pathlist()

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

Definition at line 785 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().

786 {
787  Relids required_outer;
788  Path *path;
789 
790  /*
791  * We don't support pushing join clauses into the quals of a samplescan,
792  * but it could still have required parameterization due to LATERAL refs
793  * in its tlist or TABLESAMPLE arguments.
794  */
795  required_outer = rel->lateral_relids;
796 
797  /* Consider sampled scan */
798  path = create_samplescan_path(root, rel, required_outer);
799 
800  /*
801  * If the sampling method does not support repeatable scans, we must avoid
802  * plans that would scan the rel multiple times. Ideally, we'd simply
803  * avoid putting the rel on the inside of a nestloop join; but adding such
804  * a consideration to the planner seems like a great deal of complication
805  * to support an uncommon usage of second-rate sampling methods. Instead,
806  * if there is a risk that the query might perform an unsafe join, just
807  * wrap the SampleScan in a Materialize node. We can check for joins by
808  * counting the membership of all_baserels (note that this correctly
809  * counts inheritance trees as single rels). If we're inside a subquery,
810  * we can't easily check whether a join might occur in the outer query, so
811  * just assume one is possible.
812  *
813  * GetTsmRoutine is relatively expensive compared to the other tests here,
814  * so check repeatable_across_scans last, even though that's a bit odd.
815  */
816  if ((root->query_level > 1 ||
819  {
820  path = (Path *) create_material_path(rel, path);
821  }
822 
823  add_path(rel, path);
824 
825  /* For the moment, at least, there are no other paths to consider */
826 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
MaterialPath * create_material_path(RelOptInfo *rel, Path *subpath)
Definition: pathnode.c:1483
Relids lateral_relids
Definition: relation.h:637
Relids all_baserels
Definition: relation.h:210
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:700
Path * create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:979
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
bool repeatable_across_scans
Definition: tsmapi.h:64
Index query_level
Definition: relation.h:173
struct TableSampleClause * tablesample
Definition: parsenodes.h:980

◆ set_tablesample_rel_size()

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

Definition at line 745 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().

746 {
747  TableSampleClause *tsc = rte->tablesample;
748  TsmRoutine *tsm;
749  BlockNumber pages;
750  double tuples;
751 
752  /*
753  * Test any partial indexes of rel for applicability. We must do this
754  * first since partial unique indexes can affect size estimates.
755  */
756  check_index_predicates(root, rel);
757 
758  /*
759  * Call the sampling method's estimation function to estimate the number
760  * of pages it will read and the number of tuples it will return. (Note:
761  * we assume the function returns sane values.)
762  */
763  tsm = GetTsmRoutine(tsc->tsmhandler);
764  tsm->SampleScanGetSampleSize(root, rel, tsc->args,
765  &pages, &tuples);
766 
767  /*
768  * For the moment, because we will only consider a SampleScan path for the
769  * rel, it's okay to just overwrite the pages and tuples estimates for the
770  * whole relation. If we ever consider multiple path types for sampled
771  * rels, we'll need more complication.
772  */
773  rel->pages = pages;
774  rel->tuples = tuples;
775 
776  /* Mark rel with estimated output rows, width, etc */
777  set_baserel_size_estimates(root, rel);
778 }
double tuples
Definition: relation.h:652
uint32 BlockNumber
Definition: block.h:31
void check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:2795
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:67
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4295
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
BlockNumber pages
Definition: relation.h:651
struct TableSampleClause * tablesample
Definition: parsenodes.h:980

◆ set_values_pathlist()

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

Definition at line 2351 of file allpaths.c.

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

Referenced by set_rel_pathlist().

2352 {
2353  Relids required_outer;
2354 
2355  /*
2356  * We don't support pushing join clauses into the quals of a values scan,
2357  * but it could still have required parameterization due to LATERAL refs
2358  * in the values expressions.
2359  */
2360  required_outer = rel->lateral_relids;
2361 
2362  /* Generate appropriate path */
2363  add_path(rel, create_valuesscan_path(root, rel, required_outer));
2364 }
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:1951
Relids lateral_relids
Definition: relation.h:637

◆ set_worktable_pathlist()

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

Definition at line 2491 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().

2492 {
2493  Path *ctepath;
2494  PlannerInfo *cteroot;
2495  Index levelsup;
2496  Relids required_outer;
2497 
2498  /*
2499  * We need to find the non-recursive term's path, which is in the plan
2500  * level that's processing the recursive UNION, which is one level *below*
2501  * where the CTE comes from.
2502  */
2503  levelsup = rte->ctelevelsup;
2504  if (levelsup == 0) /* shouldn't happen */
2505  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2506  levelsup--;
2507  cteroot = root;
2508  while (levelsup-- > 0)
2509  {
2510  cteroot = cteroot->parent_root;
2511  if (!cteroot) /* shouldn't happen */
2512  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2513  }
2514  ctepath = cteroot->non_recursive_path;
2515  if (!ctepath) /* shouldn't happen */
2516  elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
2517 
2518  /* Mark rel with estimated output rows, width, etc */
2519  set_cte_size_estimates(root, rel, ctepath->rows);
2520 
2521  /*
2522  * We don't support pushing join clauses into the quals of a worktable
2523  * scan, but it could still have required parameterization due to LATERAL
2524  * refs in its tlist. (I'm not sure this is actually possible given the
2525  * restrictions on recursive references, but it's easy enough to support.)
2526  */
2527  required_outer = rel->lateral_relids;
2528 
2529  /* Generate appropriate path */
2530  add_path(rel, create_worktablescan_path(root, rel, required_outer));
2531 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:4982
Relids lateral_relids
Definition: relation.h:637
#define ERROR
Definition: elog.h:43
Path * create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2028
struct PlannerInfo * parent_root
Definition: relation.h:175
unsigned int Index
Definition: c.h:442
double rows
Definition: relation.h:1088
struct Path * non_recursive_path
Definition: relation.h:325
Index ctelevelsup
Definition: parsenodes.h:1033
char * ctename
Definition: parsenodes.h:1032
#define elog
Definition: elog.h:219

◆ standard_join_search()

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

Definition at line 2711 of file allpaths.c.

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

Referenced by make_rel_from_joinlist().

2712 {
2713  int lev;
2714  RelOptInfo *rel;
2715 
2716  /*
2717  * This function cannot be invoked recursively within any one planning
2718  * problem, so join_rel_level[] can't be in use already.
2719  */
2720  Assert(root->join_rel_level == NULL);
2721 
2722  /*
2723  * We employ a simple "dynamic programming" algorithm: we first find all
2724  * ways to build joins of two jointree items, then all ways to build joins
2725  * of three items (from two-item joins and single items), then four-item
2726  * joins, and so on until we have considered all ways to join all the
2727  * items into one rel.
2728  *
2729  * root->join_rel_level[j] is a list of all the j-item rels. Initially we
2730  * set root->join_rel_level[1] to represent all the single-jointree-item
2731  * relations.
2732  */
2733  root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
2734 
2735  root->join_rel_level[1] = initial_rels;
2736 
2737  for (lev = 2; lev <= levels_needed; lev++)
2738  {
2739  ListCell *lc;
2740 
2741  /*
2742  * Determine all possible pairs of relations to be joined at this
2743  * level, and build paths for making each one from every available
2744  * pair of lower-level relations.
2745  */
2746  join_search_one_level(root, lev);
2747 
2748  /*
2749  * Run generate_partitionwise_join_paths() and generate_gather_paths()
2750  * for each just-processed joinrel. We could not do this earlier
2751  * because both regular and partial paths can get added to a
2752  * particular joinrel at multiple times within join_search_one_level.
2753  *
2754  * After that, we're done creating paths for the joinrel, so run
2755  * set_cheapest().
2756  */
2757  foreach(lc, root->join_rel_level[lev])
2758  {
2759  rel = (RelOptInfo *) lfirst(lc);
2760 
2761  /* Create paths for partitionwise joins. */
2763 
2764  /*
2765  * Except for the topmost scan/join rel, consider gathering
2766  * partial paths. We'll do the same for the topmost scan/join rel
2767  * once we know the final targetlist (see grouping_planner).
2768  */
2769  if (lev < levels_needed)
2770  generate_gather_paths(root, rel, false);
2771 
2772  /* Find and save the cheapest paths for this rel */
2773  set_cheapest(rel);
2774 
2775 #ifdef OPTIMIZER_DEBUG
2776  debug_print_rel(root, rel);
2777 #endif
2778  }
2779  }
2780 
2781  /*
2782  * We should have a single rel at the final level.
2783  */
2784  if (root->join_rel_level[levels_needed] == NIL)
2785  elog(ERROR, "failed to build any %d-way joins", levels_needed);
2786  Assert(list_length(root->join_rel_level[levels_needed]) == 1);
2787 
2788  rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
2789 
2790  root->join_rel_level = NULL;
2791 
2792  return rel;
2793 }
#define NIL
Definition: pg_list.h:69
void generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:3531
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:2550
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
void join_search_one_level(PlannerInfo *root, int level)
Definition: joinrels.c:65
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void * palloc0(Size size)
Definition: mcxt.c:955
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List ** join_rel_level
Definition: relation.h:239
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
Definition: pg_list.h:45

◆ subquery_is_pushdown_safe()

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

Definition at line 2869 of file allpaths.c.

References Assert, castNode, check_output_expressions(), SetOperationStmt::colTypes, compare_tlist_datatypes(), Query::distinctClause, 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().

2871 {
2872  SetOperationStmt *topop;
2873 
2874  /* Check point 1 */
2875  if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
2876  return false;
2877 
2878  /* Check points 3, 4, and 5 */
2879  if (subquery->distinctClause ||
2880  subquery->hasWindowFuncs ||
2881  subquery->hasTargetSRFs)
2882  safetyInfo->unsafeVolatile = true;
2883 
2884  /*
2885  * If we're at a leaf query, check for unsafe expressions in its target
2886  * list, and mark any unsafe ones in unsafeColumns[]. (Non-leaf nodes in
2887  * setop trees have only simple Vars in their tlists, so no need to check
2888  * them.)
2889  */
2890  if (subquery->setOperations == NULL)
2891  check_output_expressions(subquery, safetyInfo);
2892 
2893  /* Are we at top level, or looking at a setop component? */
2894  if (subquery == topquery)
2895  {
2896  /* Top level, so check any component queries */
2897  if (subquery->setOperations != NULL)
2898  if (!recurse_pushdown_safe(subquery->setOperations, topquery,
2899  safetyInfo))
2900  return false;
2901  }
2902  else
2903  {
2904  /* Setop component must not have more components (too weird) */
2905  if (subquery->setOperations != NULL)
2906  return false;
2907  /* Check whether setop component output types match top level */
2908  topop = castNode(SetOperationStmt, topquery->setOperations);
2909  Assert(topop);
2911  topop->colTypes,
2912  safetyInfo);
2913  }
2914  return true;
2915 }
Node * limitOffset
Definition: parsenodes.h:160
#define castNode(_type_, nodeptr)
Definition: nodes.h:585
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2921
static void compare_tlist_datatypes(List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3056
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:2988
bool hasTargetSRFs
Definition: parsenodes.h:127
#define Assert(condition)
Definition: c.h:699
bool hasWindowFuncs
Definition: parsenodes.h:126
Node * setOperations
Definition: parsenodes.h:165

◆ subquery_push_qual()

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

Definition at line 3208 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().

3209 {
3210  if (subquery->setOperations != NULL)
3211  {
3212  /* Recurse to push it separately to each component query */
3213  recurse_push_qual(subquery->setOperations, subquery,
3214  rte, rti, qual);
3215  }
3216  else
3217  {
3218  /*
3219  * We need to replace Vars in the qual (which must refer to outputs of
3220  * the subquery) with copies of the subquery's targetlist expressions.
3221  * Note that at this point, any uplevel Vars in the qual should have
3222  * been replaced with Params, so they need no work.
3223  *
3224  * This step also ensures that when we are pushing into a setop tree,
3225  * each component query gets its own copy of the qual.
3226  */
3227  qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
3228  subquery->targetList,
3230  &subquery->hasSubLinks);
3231 
3232  /*
3233  * Now attach the qual to the proper place: normally WHERE, but if the
3234  * subquery uses grouping or aggregation, put it in HAVING (since the
3235  * qual really refers to the group-result rows).
3236  */
3237  if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
3238  subquery->havingQual = make_and_qual(subquery->havingQual, qual);
3239  else
3240  subquery->jointree->quals =
3241  make_and_qual(subquery->jointree->quals, qual);
3242 
3243  /*
3244  * We need not change the subquery's hasAggs or hasSubLinks flags,
3245  * since we can't be pushing down any aggregates that weren't there
3246  * before, and we don't push down subselects at all.
3247  */
3248  }
3249 }
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: clauses.c:349
FromExpr * jointree
Definition: parsenodes.h:138
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
Node * quals
Definition: primnodes.h:1479
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:3255
Node * setOperations
Definition: parsenodes.h:165
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 3089 of file allpaths.c.

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

Referenced by check_output_expressions().

3090 {
3091  ListCell *lc;
3092 
3093  foreach(lc, query->windowClause)
3094  {
3095  WindowClause *wc = (WindowClause *) lfirst(lc);
3096 
3098  return false;
3099  }
3100  return true;
3101 }
List * windowClause
Definition: parsenodes.h:154
List * partitionClause
Definition: parsenodes.h:1303
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define lfirst(lc)
Definition: pg_list.h:106

Variable Documentation

◆ enable_geqo

bool enable_geqo = false

Definition at line 60 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ geqo_threshold

int geqo_threshold

Definition at line 61 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ join_search_hook

join_search_hook_type join_search_hook = NULL

Definition at line 69 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ min_parallel_index_scan_size

int min_parallel_index_scan_size

Definition at line 63 of file allpaths.c.

Referenced by compute_parallel_worker().

◆ min_parallel_table_scan_size

int min_parallel_table_scan_size

Definition at line 62 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 66 of file allpaths.c.

Referenced by set_rel_pathlist().