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 "nodes/supportnodes.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/inherit.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/planner.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "partitioning/partbounds.h"
#include "partitioning/partprune.h"
#include "port/pg_bitutils.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
 

Macros

#define UNSAFE_HAS_VOLATILE_FUNC   (1 << 0)
 
#define UNSAFE_HAS_SET_FUNC   (1 << 1)
 
#define UNSAFE_NOTIN_DISTINCTON_CLAUSE   (1 << 2)
 
#define UNSAFE_NOTIN_PARTITIONBY_CLAUSE   (1 << 3)
 
#define UNSAFE_TYPE_MISMATCH   (1 << 4)
 

Typedefs

typedef struct pushdown_safety_info pushdown_safety_info
 
typedef enum pushdown_safe_type pushdown_safe_type
 

Enumerations

enum  pushdown_safe_type { PUSHDOWN_UNSAFE , PUSHDOWN_SAFE , PUSHDOWN_WINDOWCLAUSE_RUNCOND }
 

Functions

static void set_base_rel_consider_startup (PlannerInfo *root)
 
static void set_base_rel_sizes (PlannerInfo *root)
 
static void set_base_rel_pathlists (PlannerInfo *root)
 
static void set_rel_size (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_plain_rel_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void create_plain_partial_paths (PlannerInfo *root, RelOptInfo *rel)
 
static void set_rel_consider_parallel (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_plain_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablesample_rel_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablesample_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_foreign_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_foreign_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_append_rel_size (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_append_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void generate_orderedappend_paths (PlannerInfo *root, RelOptInfo *rel, List *live_childrels, List *all_child_pathkeys)
 
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 Pathget_singleton_append_subpath (Path *path)
 
static void set_dummy_rel_pathlist (RelOptInfo *rel)
 
static void set_subquery_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_function_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_values_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablefunc_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_cte_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_namedtuplestore_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_result_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_worktable_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static RelOptInfomake_rel_from_joinlist (PlannerInfo *root, List *joinlist)
 
static bool subquery_is_pushdown_safe (Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
 
static bool recurse_pushdown_safe (Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
 
static void check_output_expressions (Query *subquery, pushdown_safety_info *safetyInfo)
 
static void compare_tlist_datatypes (List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
 
static bool targetIsInAllPartitionLists (TargetEntry *tle, Query *query)
 
static pushdown_safe_type qual_is_pushdown_safe (Query *subquery, Index rti, RestrictInfo *rinfo, 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, Bitmapset *extra_used_attrs)
 
RelOptInfomake_one_rel (PlannerInfo *root, List *joinlist)
 
void add_paths_to_append_rel (PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
 
static bool has_multiple_baserels (PlannerInfo *root)
 
static bool find_window_run_conditions (Query *subquery, RangeTblEntry *rte, Index rti, AttrNumber attno, WindowFunc *wfunc, OpExpr *opexpr, bool wfunc_left, bool *keep_original, Bitmapset **run_cond_attrs)
 
static bool check_and_push_window_quals (Query *subquery, RangeTblEntry *rte, Index rti, Node *clause, Bitmapset **run_cond_attrs)
 
void generate_gather_paths (PlannerInfo *root, RelOptInfo *rel, bool override_rows)
 
static Listget_useful_pathkeys_for_relation (PlannerInfo *root, RelOptInfo *rel, bool require_parallel_safe)
 
void generate_useful_gather_paths (PlannerInfo *root, RelOptInfo *rel, bool override_rows)
 
RelOptInfostandard_join_search (PlannerInfo *root, int levels_needed, List *initial_rels)
 
void create_partial_bitmap_paths (PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual)
 
int compute_parallel_worker (RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
 
void generate_partitionwise_join_paths (PlannerInfo *root, RelOptInfo *rel)
 

Variables

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

Macro Definition Documentation

◆ UNSAFE_HAS_SET_FUNC

#define UNSAFE_HAS_SET_FUNC   (1 << 1)

Definition at line 57 of file allpaths.c.

◆ UNSAFE_HAS_VOLATILE_FUNC

#define UNSAFE_HAS_VOLATILE_FUNC   (1 << 0)

Definition at line 56 of file allpaths.c.

◆ UNSAFE_NOTIN_DISTINCTON_CLAUSE

#define UNSAFE_NOTIN_DISTINCTON_CLAUSE   (1 << 2)

Definition at line 58 of file allpaths.c.

◆ UNSAFE_NOTIN_PARTITIONBY_CLAUSE

#define UNSAFE_NOTIN_PARTITIONBY_CLAUSE   (1 << 3)

Definition at line 59 of file allpaths.c.

◆ UNSAFE_TYPE_MISMATCH

#define UNSAFE_TYPE_MISMATCH   (1 << 4)

Definition at line 60 of file allpaths.c.

Typedef Documentation

◆ pushdown_safe_type

◆ pushdown_safety_info

Enumeration Type Documentation

◆ pushdown_safe_type

Enumerator
PUSHDOWN_UNSAFE 
PUSHDOWN_SAFE 
PUSHDOWN_WINDOWCLAUSE_RUNCOND 

Definition at line 73 of file allpaths.c.

74 {
75  PUSHDOWN_UNSAFE, /* unsafe to push qual into subquery */
76  PUSHDOWN_SAFE, /* safe to push qual into subquery */
77  PUSHDOWN_WINDOWCLAUSE_RUNCOND /* unsafe, but may work as WindowClause
78  * run condition */
pushdown_safe_type
Definition: allpaths.c:74
@ PUSHDOWN_WINDOWCLAUSE_RUNCOND
Definition: allpaths.c:77
@ PUSHDOWN_UNSAFE
Definition: allpaths.c:75
@ PUSHDOWN_SAFE
Definition: allpaths.c:76

Function Documentation

◆ accumulate_append_subpath()

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

Definition at line 2066 of file allpaths.c.

2067 {
2068  if (IsA(path, AppendPath))
2069  {
2070  AppendPath *apath = (AppendPath *) path;
2071 
2072  if (!apath->path.parallel_aware || apath->first_partial_path == 0)
2073  {
2074  *subpaths = list_concat(*subpaths, apath->subpaths);
2075  return;
2076  }
2077  else if (special_subpaths != NULL)
2078  {
2079  List *new_special_subpaths;
2080 
2081  /* Split Parallel Append into partial and non-partial subpaths */
2082  *subpaths = list_concat(*subpaths,
2083  list_copy_tail(apath->subpaths,
2084  apath->first_partial_path));
2085  new_special_subpaths = list_copy_head(apath->subpaths,
2086  apath->first_partial_path);
2087  *special_subpaths = list_concat(*special_subpaths,
2088  new_special_subpaths);
2089  return;
2090  }
2091  }
2092  else if (IsA(path, MergeAppendPath))
2093  {
2094  MergeAppendPath *mpath = (MergeAppendPath *) path;
2095 
2096  *subpaths = list_concat(*subpaths, mpath->subpaths);
2097  return;
2098  }
2099 
2100  *subpaths = lappend(*subpaths, path);
2101 }
List * lappend(List *list, void *datum)
Definition: list.c:338
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1592
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1612
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
int first_partial_path
Definition: pathnodes.h:1902
List * subpaths
Definition: pathnodes.h:1900
Definition: pg_list.h:54
bool parallel_aware
Definition: pathnodes.h:1621

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

Referenced by add_paths_to_append_rel(), and generate_orderedappend_paths().

◆ add_paths_to_append_rel()

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

Definition at line 1305 of file allpaths.c.

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

References accumulate_append_subpath(), add_partial_path(), add_path(), Assert(), bms_equal(), RelOptInfo::cheapest_total_path, compare_pathkeys(), RelOptInfo::consider_parallel, create_append_path(), enable_parallel_append, for_each_from, generate_orderedappend_paths(), get_cheapest_parallel_safe_total_inner(), get_cheapest_parameterized_child_path(), lappend(), lfirst, linitial, list_length(), list_make1, Max, max_parallel_workers_per_gather, Min, NIL, Path::parallel_workers, RelOptInfo::partial_pathlist, AppendPath::path, PATH_REQ_OUTER, Path::pathkeys, PATHKEYS_EQUAL, RelOptInfo::pathlist, pg_leftmost_one_pos32(), Path::rows, 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().

◆ check_and_push_window_quals()

static bool check_and_push_window_quals ( Query subquery,
RangeTblEntry rte,
Index  rti,
Node clause,
Bitmapset **  run_cond_attrs 
)
static

Definition at line 2420 of file allpaths.c.

2422 {
2423  OpExpr *opexpr = (OpExpr *) clause;
2424  bool keep_original = true;
2425  Var *var1;
2426  Var *var2;
2427 
2428  /* We're only able to use OpExprs with 2 operands */
2429  if (!IsA(opexpr, OpExpr))
2430  return true;
2431 
2432  if (list_length(opexpr->args) != 2)
2433  return true;
2434 
2435  /*
2436  * Currently, we restrict this optimization to strict OpExprs. The reason
2437  * for this is that during execution, once the runcondition becomes false,
2438  * we stop evaluating WindowFuncs. To avoid leaving around stale window
2439  * function result values, we set them to NULL. Having only strict
2440  * OpExprs here ensures that we properly filter out the tuples with NULLs
2441  * in the top-level WindowAgg.
2442  */
2443  set_opfuncid(opexpr);
2444  if (!func_strict(opexpr->opfuncid))
2445  return true;
2446 
2447  /*
2448  * Check for plain Vars that reference window functions in the subquery.
2449  * If we find any, we'll ask find_window_run_conditions() if 'opexpr' can
2450  * be used as part of the run condition.
2451  */
2452 
2453  /* Check the left side of the OpExpr */
2454  var1 = linitial(opexpr->args);
2455  if (IsA(var1, Var) && var1->varattno > 0)
2456  {
2457  TargetEntry *tle = list_nth(subquery->targetList, var1->varattno - 1);
2458  WindowFunc *wfunc = (WindowFunc *) tle->expr;
2459 
2460  if (find_window_run_conditions(subquery, rte, rti, tle->resno, wfunc,
2461  opexpr, true, &keep_original,
2462  run_cond_attrs))
2463  return keep_original;
2464  }
2465 
2466  /* and check the right side */
2467  var2 = lsecond(opexpr->args);
2468  if (IsA(var2, Var) && var2->varattno > 0)
2469  {
2470  TargetEntry *tle = list_nth(subquery->targetList, var2->varattno - 1);
2471  WindowFunc *wfunc = (WindowFunc *) tle->expr;
2472 
2473  if (find_window_run_conditions(subquery, rte, rti, tle->resno, wfunc,
2474  opexpr, false, &keep_original,
2475  run_cond_attrs))
2476  return keep_original;
2477  }
2478 
2479  return true;
2480 }
static bool find_window_run_conditions(Query *subquery, RangeTblEntry *rte, Index rti, AttrNumber attno, WindowFunc *wfunc, OpExpr *opexpr, bool wfunc_left, bool *keep_original, Bitmapset **run_cond_attrs)
Definition: allpaths.c:2215
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
bool func_strict(Oid funcid)
Definition: lsyscache.c:1765
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1779
#define lsecond(l)
Definition: pg_list.h:183
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
List * args
Definition: primnodes.h:763
List * targetList
Definition: parsenodes.h:188
Expr * expr
Definition: primnodes.h:1895
AttrNumber resno
Definition: primnodes.h:1897
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238

References OpExpr::args, TargetEntry::expr, find_window_run_conditions(), func_strict(), if(), IsA, linitial, list_length(), list_nth(), lsecond, TargetEntry::resno, set_opfuncid(), Query::targetList, and Var::varattno.

Referenced by set_subquery_pathlist().

◆ check_output_expressions()

static void check_output_expressions ( Query subquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3715 of file allpaths.c.

3716 {
3717  ListCell *lc;
3718 
3719  foreach(lc, subquery->targetList)
3720  {
3721  TargetEntry *tle = (TargetEntry *) lfirst(lc);
3722 
3723  if (tle->resjunk)
3724  continue; /* ignore resjunk columns */
3725 
3726  /* Functions returning sets are unsafe (point 1) */
3727  if (subquery->hasTargetSRFs &&
3728  (safetyInfo->unsafeFlags[tle->resno] &
3729  UNSAFE_HAS_SET_FUNC) == 0 &&
3730  expression_returns_set((Node *) tle->expr))
3731  {
3732  safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_HAS_SET_FUNC;
3733  continue;
3734  }
3735 
3736  /* Volatile functions are unsafe (point 2) */
3737  if ((safetyInfo->unsafeFlags[tle->resno] &
3738  UNSAFE_HAS_VOLATILE_FUNC) == 0 &&
3740  {
3741  safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_HAS_VOLATILE_FUNC;
3742  continue;
3743  }
3744 
3745  /* If subquery uses DISTINCT ON, check point 3 */
3746  if (subquery->hasDistinctOn &&
3747  (safetyInfo->unsafeFlags[tle->resno] &
3749  !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
3750  {
3751  /* non-DISTINCT column, so mark it unsafe */
3752  safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_NOTIN_DISTINCTON_CLAUSE;
3753  continue;
3754  }
3755 
3756  /* If subquery uses window functions, check point 4 */
3757  if (subquery->hasWindowFuncs &&
3758  (safetyInfo->unsafeFlags[tle->resno] &
3760  !targetIsInAllPartitionLists(tle, subquery))
3761  {
3762  /* not present in all PARTITION BY clauses, so mark it unsafe */
3763  safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_NOTIN_PARTITIONBY_CLAUSE;
3764  continue;
3765  }
3766  }
3767 }
#define UNSAFE_HAS_VOLATILE_FUNC
Definition: allpaths.c:56
#define UNSAFE_NOTIN_DISTINCTON_CLAUSE
Definition: allpaths.c:58
static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
Definition: allpaths.c:3820
#define UNSAFE_HAS_SET_FUNC
Definition: allpaths.c:57
#define UNSAFE_NOTIN_PARTITIONBY_CLAUSE
Definition: allpaths.c:59
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:483
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:728
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define InvalidOid
Definition: postgres_ext.h:36
Definition: nodes.h:129
List * distinctClause
Definition: parsenodes.h:206
unsigned char * unsafeFlags
Definition: allpaths.c:65

References contain_volatile_functions(), Query::distinctClause, TargetEntry::expr, expression_returns_set(), InvalidOid, lfirst, TargetEntry::resno, targetIsInAllPartitionLists(), targetIsInSortList(), Query::targetList, UNSAFE_HAS_SET_FUNC, UNSAFE_HAS_VOLATILE_FUNC, UNSAFE_NOTIN_DISTINCTON_CLAUSE, UNSAFE_NOTIN_PARTITIONBY_CLAUSE, and pushdown_safety_info::unsafeFlags.

Referenced by subquery_is_pushdown_safe().

◆ compare_tlist_datatypes()

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

Definition at line 3787 of file allpaths.c.

3789 {
3790  ListCell *l;
3791  ListCell *colType = list_head(colTypes);
3792 
3793  foreach(l, tlist)
3794  {
3795  TargetEntry *tle = (TargetEntry *) lfirst(l);
3796 
3797  if (tle->resjunk)
3798  continue; /* ignore resjunk columns */
3799  if (colType == NULL)
3800  elog(ERROR, "wrong number of tlist entries");
3801  if (exprType((Node *) tle->expr) != lfirst_oid(colType))
3802  safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_TYPE_MISMATCH;
3803  colType = lnext(colTypes, colType);
3804  }
3805  if (colType != NULL)
3806  elog(ERROR, "wrong number of tlist entries");
3807 }
#define UNSAFE_TYPE_MISMATCH
Definition: allpaths.c:60
#define ERROR
Definition: elog.h:39
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define lfirst_oid(lc)
Definition: pg_list.h:174

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

Referenced by subquery_is_pushdown_safe().

◆ compute_parallel_worker()

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

Definition at line 4211 of file allpaths.c.

4213 {
4214  int parallel_workers = 0;
4215 
4216  /*
4217  * If the user has set the parallel_workers reloption, use that; otherwise
4218  * select a default number of workers.
4219  */
4220  if (rel->rel_parallel_workers != -1)
4221  parallel_workers = rel->rel_parallel_workers;
4222  else
4223  {
4224  /*
4225  * If the number of pages being scanned is insufficient to justify a
4226  * parallel scan, just return zero ... unless it's an inheritance
4227  * child. In that case, we want to generate a parallel path here
4228  * anyway. It might not be worthwhile just for this relation, but
4229  * when combined with all of its inheritance siblings it may well pay
4230  * off.
4231  */
4232  if (rel->reloptkind == RELOPT_BASEREL &&
4233  ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
4234  (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
4235  return 0;
4236 
4237  if (heap_pages >= 0)
4238  {
4239  int heap_parallel_threshold;
4240  int heap_parallel_workers = 1;
4241 
4242  /*
4243  * Select the number of workers based on the log of the size of
4244  * the relation. This probably needs to be a good deal more
4245  * sophisticated, but we need something here for now. Note that
4246  * the upper limit of the min_parallel_table_scan_size GUC is
4247  * chosen to prevent overflow here.
4248  */
4249  heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
4250  while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
4251  {
4252  heap_parallel_workers++;
4253  heap_parallel_threshold *= 3;
4254  if (heap_parallel_threshold > INT_MAX / 3)
4255  break; /* avoid overflow */
4256  }
4257 
4258  parallel_workers = heap_parallel_workers;
4259  }
4260 
4261  if (index_pages >= 0)
4262  {
4263  int index_parallel_workers = 1;
4264  int index_parallel_threshold;
4265 
4266  /* same calculation as for heap_pages above */
4267  index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
4268  while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
4269  {
4270  index_parallel_workers++;
4271  index_parallel_threshold *= 3;
4272  if (index_parallel_threshold > INT_MAX / 3)
4273  break; /* avoid overflow */
4274  }
4275 
4276  if (parallel_workers > 0)
4277  parallel_workers = Min(parallel_workers, index_parallel_workers);
4278  else
4279  parallel_workers = index_parallel_workers;
4280  }
4281  }
4282 
4283  /* In no case use more than caller supplied maximum number of workers */
4284  parallel_workers = Min(parallel_workers, max_workers);
4285 
4286  return parallel_workers;
4287 }
int min_parallel_index_scan_size
Definition: allpaths.c:85
int min_parallel_table_scan_size
Definition: allpaths.c:84
uint32 BlockNumber
Definition: block.h:31
@ RELOPT_BASEREL
Definition: pathnodes.h:812
RelOptKind reloptkind
Definition: pathnodes.h:850
int rel_parallel_workers
Definition: pathnodes.h:935

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

◆ create_partial_bitmap_paths()

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

Definition at line 4175 of file allpaths.c.

4177 {
4178  int parallel_workers;
4179  double pages_fetched;
4180 
4181  /* Compute heap pages for bitmap heap scan */
4182  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
4183  NULL, NULL);
4184 
4185  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1,
4187 
4188  if (parallel_workers <= 0)
4189  return;
4190 
4191  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
4192  bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
4193 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:4211
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:6400
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1044
Relids lateral_relids
Definition: pathnodes.h:898

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

◆ create_plain_partial_paths()

static void create_plain_partial_paths ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 797 of file allpaths.c.

798 {
799  int parallel_workers;
800 
801  parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
803 
804  /* If any limit was set to zero, the user doesn't want a parallel scan. */
805  if (parallel_workers <= 0)
806  return;
807 
808  /* Add an unordered partial path based on a parallel sequential scan. */
809  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
810 }
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:929
BlockNumber pages
Definition: pathnodes.h:927

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

Referenced by set_plain_rel_pathlist().

◆ find_window_run_conditions()

static bool find_window_run_conditions ( Query subquery,
RangeTblEntry rte,
Index  rti,
AttrNumber  attno,
WindowFunc wfunc,
OpExpr opexpr,
bool  wfunc_left,
bool keep_original,
Bitmapset **  run_cond_attrs 
)
static

Definition at line 2215 of file allpaths.c.

2219 {
2220  Oid prosupport;
2221  Expr *otherexpr;
2224  WindowClause *wclause;
2225  List *opinfos;
2226  OpExpr *runopexpr;
2227  Oid runoperator;
2228  ListCell *lc;
2229 
2230  *keep_original = true;
2231 
2232  while (IsA(wfunc, RelabelType))
2233  wfunc = (WindowFunc *) ((RelabelType *) wfunc)->arg;
2234 
2235  /* we can only work with window functions */
2236  if (!IsA(wfunc, WindowFunc))
2237  return false;
2238 
2239  /* can't use it if there are subplans in the WindowFunc */
2240  if (contain_subplans((Node *) wfunc))
2241  return false;
2242 
2243  prosupport = get_func_support(wfunc->winfnoid);
2244 
2245  /* Check if there's a support function for 'wfunc' */
2246  if (!OidIsValid(prosupport))
2247  return false;
2248 
2249  /* get the Expr from the other side of the OpExpr */
2250  if (wfunc_left)
2251  otherexpr = lsecond(opexpr->args);
2252  else
2253  otherexpr = linitial(opexpr->args);
2254 
2255  /*
2256  * The value being compared must not change during the evaluation of the
2257  * window partition.
2258  */
2259  if (!is_pseudo_constant_clause((Node *) otherexpr))
2260  return false;
2261 
2262  /* find the window clause belonging to the window function */
2263  wclause = (WindowClause *) list_nth(subquery->windowClause,
2264  wfunc->winref - 1);
2265 
2266  req.type = T_SupportRequestWFuncMonotonic;
2267  req.window_func = wfunc;
2268  req.window_clause = wclause;
2269 
2270  /* call the support function */
2272  DatumGetPointer(OidFunctionCall1(prosupport,
2273  PointerGetDatum(&req)));
2274 
2275  /*
2276  * Nothing to do if the function is neither monotonically increasing nor
2277  * monotonically decreasing.
2278  */
2279  if (res == NULL || res->monotonic == MONOTONICFUNC_NONE)
2280  return false;
2281 
2282  runopexpr = NULL;
2283  runoperator = InvalidOid;
2284  opinfos = get_op_btree_interpretation(opexpr->opno);
2285 
2286  foreach(lc, opinfos)
2287  {
2289  int strategy = opinfo->strategy;
2290 
2291  /* handle < / <= */
2292  if (strategy == BTLessStrategyNumber ||
2293  strategy == BTLessEqualStrategyNumber)
2294  {
2295  /*
2296  * < / <= is supported for monotonically increasing functions in
2297  * the form <wfunc> op <pseudoconst> and <pseudoconst> op <wfunc>
2298  * for monotonically decreasing functions.
2299  */
2300  if ((wfunc_left && (res->monotonic & MONOTONICFUNC_INCREASING)) ||
2301  (!wfunc_left && (res->monotonic & MONOTONICFUNC_DECREASING)))
2302  {
2303  *keep_original = false;
2304  runopexpr = opexpr;
2305  runoperator = opexpr->opno;
2306  }
2307  break;
2308  }
2309  /* handle > / >= */
2310  else if (strategy == BTGreaterStrategyNumber ||
2311  strategy == BTGreaterEqualStrategyNumber)
2312  {
2313  /*
2314  * > / >= is supported for monotonically decreasing functions in
2315  * the form <wfunc> op <pseudoconst> and <pseudoconst> op <wfunc>
2316  * for monotonically increasing functions.
2317  */
2318  if ((wfunc_left && (res->monotonic & MONOTONICFUNC_DECREASING)) ||
2319  (!wfunc_left && (res->monotonic & MONOTONICFUNC_INCREASING)))
2320  {
2321  *keep_original = false;
2322  runopexpr = opexpr;
2323  runoperator = opexpr->opno;
2324  }
2325  break;
2326  }
2327  /* handle = */
2328  else if (strategy == BTEqualStrategyNumber)
2329  {
2330  int16 newstrategy;
2331 
2332  /*
2333  * When both monotonically increasing and decreasing then the
2334  * return value of the window function will be the same each time.
2335  * We can simply use 'opexpr' as the run condition without
2336  * modifying it.
2337  */
2338  if ((res->monotonic & MONOTONICFUNC_BOTH) == MONOTONICFUNC_BOTH)
2339  {
2340  *keep_original = false;
2341  runopexpr = opexpr;
2342  runoperator = opexpr->opno;
2343  break;
2344  }
2345 
2346  /*
2347  * When monotonically increasing we make a qual with <wfunc> <=
2348  * <value> or <value> >= <wfunc> in order to filter out values
2349  * which are above the value in the equality condition. For
2350  * monotonically decreasing functions we want to filter values
2351  * below the value in the equality condition.
2352  */
2353  if (res->monotonic & MONOTONICFUNC_INCREASING)
2354  newstrategy = wfunc_left ? BTLessEqualStrategyNumber : BTGreaterEqualStrategyNumber;
2355  else
2356  newstrategy = wfunc_left ? BTGreaterEqualStrategyNumber : BTLessEqualStrategyNumber;
2357 
2358  /* We must keep the original equality qual */
2359  *keep_original = true;
2360  runopexpr = opexpr;
2361 
2362  /* determine the operator to use for the runCondition qual */
2363  runoperator = get_opfamily_member(opinfo->opfamily_id,
2364  opinfo->oplefttype,
2365  opinfo->oprighttype,
2366  newstrategy);
2367  break;
2368  }
2369  }
2370 
2371  if (runopexpr != NULL)
2372  {
2373  Expr *newexpr;
2374 
2375  /*
2376  * Build the qual required for the run condition keeping the
2377  * WindowFunc on the same side as it was originally.
2378  */
2379  if (wfunc_left)
2380  newexpr = make_opclause(runoperator,
2381  runopexpr->opresulttype,
2382  runopexpr->opretset, (Expr *) wfunc,
2383  otherexpr, runopexpr->opcollid,
2384  runopexpr->inputcollid);
2385  else
2386  newexpr = make_opclause(runoperator,
2387  runopexpr->opresulttype,
2388  runopexpr->opretset,
2389  otherexpr, (Expr *) wfunc,
2390  runopexpr->opcollid,
2391  runopexpr->inputcollid);
2392 
2393  wclause->runCondition = lappend(wclause->runCondition, newexpr);
2394 
2395  /* record that this attno was used in a run condition */
2396  *run_cond_attrs = bms_add_member(*run_cond_attrs,
2398  return true;
2399  }
2400 
2401  /* unsupported OpExpr */
2402  return false;
2403 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:753
signed short int16
Definition: c.h:482
#define OidIsValid(objectId)
Definition: c.h:764
bool is_pseudo_constant_clause(Node *clause)
Definition: clauses.c:2005
bool contain_subplans(Node *clause)
Definition: clauses.c:332
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:680
RegProcedure get_func_support(Oid funcid)
Definition: lsyscache.c:1862
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:165
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:600
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:613
void * arg
@ MONOTONICFUNC_NONE
Definition: plannodes.h:1585
@ MONOTONICFUNC_DECREASING
Definition: plannodes.h:1587
@ MONOTONICFUNC_INCREASING
Definition: plannodes.h:1586
@ MONOTONICFUNC_BOTH
Definition: plannodes.h:1588
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
unsigned int Oid
Definition: postgres_ext.h:31
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Oid opno
Definition: primnodes.h:745
List * windowClause
Definition: parsenodes.h:204
struct WindowClause * window_clause
Definition: supportnodes.h:296
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References arg, OpExpr::args, bms_add_member(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, contain_subplans(), DatumGetPointer(), FirstLowInvalidHeapAttributeNumber, get_func_support(), get_op_btree_interpretation(), get_opfamily_member(), InvalidOid, is_pseudo_constant_clause(), IsA, lappend(), lfirst, linitial, list_nth(), lsecond, make_opclause(), MONOTONICFUNC_BOTH, MONOTONICFUNC_DECREASING, MONOTONICFUNC_INCREASING, MONOTONICFUNC_NONE, OidFunctionCall1, OidIsValid, OpBtreeInterpretation::opfamily_id, OpBtreeInterpretation::oplefttype, OpExpr::opno, OpBtreeInterpretation::oprighttype, PointerGetDatum(), res, OpBtreeInterpretation::strategy, SupportRequestWFuncMonotonic::type, SupportRequestWFuncMonotonic::window_clause, SupportRequestWFuncMonotonic::window_func, Query::windowClause, WindowFunc::winfnoid, and WindowFunc::winref.

Referenced by check_and_push_window_quals().

◆ generate_gather_paths()

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

Definition at line 3060 of file allpaths.c.

3061 {
3062  Path *cheapest_partial_path;
3063  Path *simple_gather_path;
3064  ListCell *lc;
3065  double rows;
3066  double *rowsp = NULL;
3067 
3068  /* If there are no partial paths, there's nothing to do here. */
3069  if (rel->partial_pathlist == NIL)
3070  return;
3071 
3072  /* Should we override the rel's rowcount estimate? */
3073  if (override_rows)
3074  rowsp = &rows;
3075 
3076  /*
3077  * The output of Gather is always unsorted, so there's only one partial
3078  * path of interest: the cheapest one. That will be the one at the front
3079  * of partial_pathlist because of the way add_partial_path works.
3080  */
3081  cheapest_partial_path = linitial(rel->partial_pathlist);
3082  rows =
3083  cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
3084  simple_gather_path = (Path *)
3085  create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
3086  NULL, rowsp);
3087  add_path(rel, simple_gather_path);
3088 
3089  /*
3090  * For each useful ordering, we can consider an order-preserving Gather
3091  * Merge.
3092  */
3093  foreach(lc, rel->partial_pathlist)
3094  {
3095  Path *subpath = (Path *) lfirst(lc);
3096  GatherMergePath *path;
3097 
3098  if (subpath->pathkeys == NIL)
3099  continue;
3100 
3101  rows = subpath->rows * subpath->parallel_workers;
3102  path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
3103  subpath->pathkeys, NULL, rowsp);
3104  add_path(rel, &path->path);
3105  }
3106 }
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1873
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
Definition: pathnode.c:1964
struct PathTarget * reltarget
Definition: pathnodes.h:878

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

Referenced by create_partial_distinct_paths(), and generate_useful_gather_paths().

◆ generate_orderedappend_paths()

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

Definition at line 1693 of file allpaths.c.

1696 {
1697  ListCell *lcp;
1698  List *partition_pathkeys = NIL;
1699  List *partition_pathkeys_desc = NIL;
1700  bool partition_pathkeys_partial = true;
1701  bool partition_pathkeys_desc_partial = true;
1702 
1703  /*
1704  * Some partitioned table setups may allow us to use an Append node
1705  * instead of a MergeAppend. This is possible in cases such as RANGE
1706  * partitioned tables where it's guaranteed that an earlier partition must
1707  * contain rows which come earlier in the sort order. To detect whether
1708  * this is relevant, build pathkey descriptions of the partition ordering,
1709  * for both forward and reverse scans.
1710  */
1711  if (rel->part_scheme != NULL && IS_SIMPLE_REL(rel) &&
1712  partitions_are_ordered(rel->boundinfo, rel->live_parts))
1713  {
1714  partition_pathkeys = build_partition_pathkeys(root, rel,
1716  &partition_pathkeys_partial);
1717 
1718  partition_pathkeys_desc = build_partition_pathkeys(root, rel,
1720  &partition_pathkeys_desc_partial);
1721 
1722  /*
1723  * You might think we should truncate_useless_pathkeys here, but
1724  * allowing partition keys which are a subset of the query's pathkeys
1725  * can often be useful. For example, consider a table partitioned by
1726  * RANGE (a, b), and a query with ORDER BY a, b, c. If we have child
1727  * paths that can produce the a, b, c ordering (perhaps via indexes on
1728  * (a, b, c)) then it works to consider the appendrel output as
1729  * ordered by a, b, c.
1730  */
1731  }
1732 
1733  /* Now consider each interesting sort ordering */
1734  foreach(lcp, all_child_pathkeys)
1735  {
1736  List *pathkeys = (List *) lfirst(lcp);
1737  List *startup_subpaths = NIL;
1738  List *total_subpaths = NIL;
1739  List *fractional_subpaths = NIL;
1740  bool startup_neq_total = false;
1741  bool match_partition_order;
1742  bool match_partition_order_desc;
1743  int end_index;
1744  int first_index;
1745  int direction;
1746 
1747  /*
1748  * Determine if this sort ordering matches any partition pathkeys we
1749  * have, for both ascending and descending partition order. If the
1750  * partition pathkeys happen to be contained in pathkeys then it still
1751  * works, as described above, providing that the partition pathkeys
1752  * are complete and not just a prefix of the partition keys. (In such
1753  * cases we'll be relying on the child paths to have sorted the
1754  * lower-order columns of the required pathkeys.)
1755  */
1756  match_partition_order =
1757  pathkeys_contained_in(pathkeys, partition_pathkeys) ||
1758  (!partition_pathkeys_partial &&
1759  pathkeys_contained_in(partition_pathkeys, pathkeys));
1760 
1761  match_partition_order_desc = !match_partition_order &&
1762  (pathkeys_contained_in(pathkeys, partition_pathkeys_desc) ||
1763  (!partition_pathkeys_desc_partial &&
1764  pathkeys_contained_in(partition_pathkeys_desc, pathkeys)));
1765 
1766  /*
1767  * When the required pathkeys match the reverse of the partition
1768  * order, we must build the list of paths in reverse starting with the
1769  * last matching partition first. We can get away without making any
1770  * special cases for this in the loop below by just looping backward
1771  * over the child relations in this case.
1772  */
1773  if (match_partition_order_desc)
1774  {
1775  /* loop backward */
1776  first_index = list_length(live_childrels) - 1;
1777  end_index = -1;
1778  direction = -1;
1779 
1780  /*
1781  * Set this to true to save us having to check for
1782  * match_partition_order_desc in the loop below.
1783  */
1784  match_partition_order = true;
1785  }
1786  else
1787  {
1788  /* for all other case, loop forward */
1789  first_index = 0;
1790  end_index = list_length(live_childrels);
1791  direction = 1;
1792  }
1793 
1794  /* Select the child paths for this ordering... */
1795  for (int i = first_index; i != end_index; i += direction)
1796  {
1797  RelOptInfo *childrel = list_nth_node(RelOptInfo, live_childrels, i);
1798  Path *cheapest_startup,
1799  *cheapest_total,
1800  *cheapest_fractional = NULL;
1801 
1802  /* Locate the right paths, if they are available. */
1803  cheapest_startup =
1805  pathkeys,
1806  NULL,
1807  STARTUP_COST,
1808  false);
1809  cheapest_total =
1811  pathkeys,
1812  NULL,
1813  TOTAL_COST,
1814  false);
1815 
1816  /*
1817  * If we can't find any paths with the right order just use the
1818  * cheapest-total path; we'll have to sort it later.
1819  */
1820  if (cheapest_startup == NULL || cheapest_total == NULL)
1821  {
1822  cheapest_startup = cheapest_total =
1823  childrel->cheapest_total_path;
1824  /* Assert we do have an unparameterized path for this child */
1825  Assert(cheapest_total->param_info == NULL);
1826  }
1827 
1828  /*
1829  * When building a fractional path, determine a cheapest
1830  * fractional path for each child relation too. Looking at startup
1831  * and total costs is not enough, because the cheapest fractional
1832  * path may be dominated by two separate paths (one for startup,
1833  * one for total).
1834  *
1835  * When needed (building fractional path), determine the cheapest
1836  * fractional path too.
1837  */
1838  if (root->tuple_fraction > 0)
1839  {
1840  double path_fraction = (1.0 / root->tuple_fraction);
1841 
1842  cheapest_fractional =
1844  pathkeys,
1845  NULL,
1846  path_fraction);
1847 
1848  /*
1849  * If we found no path with matching pathkeys, use the
1850  * cheapest total path instead.
1851  *
1852  * XXX We might consider partially sorted paths too (with an
1853  * incremental sort on top). But we'd have to build all the
1854  * incremental paths, do the costing etc.
1855  */
1856  if (!cheapest_fractional)
1857  cheapest_fractional = cheapest_total;
1858  }
1859 
1860  /*
1861  * Notice whether we actually have different paths for the
1862  * "cheapest" and "total" cases; frequently there will be no point
1863  * in two create_merge_append_path() calls.
1864  */
1865  if (cheapest_startup != cheapest_total)
1866  startup_neq_total = true;
1867 
1868  /*
1869  * Collect the appropriate child paths. The required logic varies
1870  * for the Append and MergeAppend cases.
1871  */
1872  if (match_partition_order)
1873  {
1874  /*
1875  * We're going to make a plain Append path. We don't need
1876  * most of what accumulate_append_subpath would do, but we do
1877  * want to cut out child Appends or MergeAppends if they have
1878  * just a single subpath (and hence aren't doing anything
1879  * useful).
1880  */
1881  cheapest_startup = get_singleton_append_subpath(cheapest_startup);
1882  cheapest_total = get_singleton_append_subpath(cheapest_total);
1883 
1884  startup_subpaths = lappend(startup_subpaths, cheapest_startup);
1885  total_subpaths = lappend(total_subpaths, cheapest_total);
1886 
1887  if (cheapest_fractional)
1888  {
1889  cheapest_fractional = get_singleton_append_subpath(cheapest_fractional);
1890  fractional_subpaths = lappend(fractional_subpaths, cheapest_fractional);
1891  }
1892  }
1893  else
1894  {
1895  /*
1896  * Otherwise, rely on accumulate_append_subpath to collect the
1897  * child paths for the MergeAppend.
1898  */
1899  accumulate_append_subpath(cheapest_startup,
1900  &startup_subpaths, NULL);
1901  accumulate_append_subpath(cheapest_total,
1902  &total_subpaths, NULL);
1903 
1904  if (cheapest_fractional)
1905  accumulate_append_subpath(cheapest_fractional,
1906  &fractional_subpaths, NULL);
1907  }
1908  }
1909 
1910  /* ... and build the Append or MergeAppend paths */
1911  if (match_partition_order)
1912  {
1913  /* We only need Append */
1914  add_path(rel, (Path *) create_append_path(root,
1915  rel,
1916  startup_subpaths,
1917  NIL,
1918  pathkeys,
1919  NULL,
1920  0,
1921  false,
1922  -1));
1923  if (startup_neq_total)
1924  add_path(rel, (Path *) create_append_path(root,
1925  rel,
1926  total_subpaths,
1927  NIL,
1928  pathkeys,
1929  NULL,
1930  0,
1931  false,
1932  -1));
1933 
1934  if (fractional_subpaths)
1935  add_path(rel, (Path *) create_append_path(root,
1936  rel,
1937  fractional_subpaths,
1938  NIL,
1939  pathkeys,
1940  NULL,
1941  0,
1942  false,
1943  -1));
1944  }
1945  else
1946  {
1947  /* We need MergeAppend */
1948  add_path(rel, (Path *) create_merge_append_path(root,
1949  rel,
1950  startup_subpaths,
1951  pathkeys,
1952  NULL));
1953  if (startup_neq_total)
1954  add_path(rel, (Path *) create_merge_append_path(root,
1955  rel,
1956  total_subpaths,
1957  pathkeys,
1958  NULL));
1959 
1960  if (fractional_subpaths)
1961  add_path(rel, (Path *) create_merge_append_path(root,
1962  rel,
1963  fractional_subpaths,
1964  pathkeys,
1965  NULL));
1966  }
1967  }
1968 }
static Path * get_singleton_append_subpath(Path *path)
Definition: allpaths.c:2111
int i
Definition: isn.c:73
bool partitions_are_ordered(PartitionBoundInfo boundinfo, Bitmapset *live_parts)
Definition: partbounds.c:2853
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:421
Path * get_cheapest_fractional_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, double fraction)
Definition: pathkeys.c:467
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:340
List * build_partition_pathkeys(PlannerInfo *root, RelOptInfo *partrel, ScanDirection scandir, bool *partialkeys)
Definition: pathkeys.c:720
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1413
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:824
@ TOTAL_COST
Definition: pathnodes.h:38
@ STARTUP_COST
Definition: pathnodes.h:38
#define list_nth_node(type, list, n)
Definition: pg_list.h:327
@ BackwardScanDirection
Definition: sdir.h:26
@ ForwardScanDirection
Definition: sdir.h:28
Selectivity tuple_fraction
Definition: pathnodes.h:478
Bitmapset * live_parts
Definition: pathnodes.h:1018

References accumulate_append_subpath(), add_path(), Assert(), BackwardScanDirection, build_partition_pathkeys(), RelOptInfo::cheapest_total_path, create_append_path(), create_merge_append_path(), ForwardScanDirection, get_cheapest_fractional_path_for_pathkeys(), get_cheapest_path_for_pathkeys(), get_singleton_append_subpath(), i, IS_SIMPLE_REL, lappend(), lfirst, list_length(), list_nth_node, RelOptInfo::live_parts, NIL, partitions_are_ordered(), pathkeys_contained_in(), RelOptInfo::pathlist, STARTUP_COST, TOTAL_COST, and PlannerInfo::tuple_fraction.

Referenced by add_paths_to_append_rel().

◆ generate_partitionwise_join_paths()

void generate_partitionwise_join_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 4299 of file allpaths.c.

4300 {
4301  List *live_children = NIL;
4302  int cnt_parts;
4303  int num_parts;
4304  RelOptInfo **part_rels;
4305 
4306  /* Handle only join relations here. */
4307  if (!IS_JOIN_REL(rel))
4308  return;
4309 
4310  /* We've nothing to do if the relation is not partitioned. */
4311  if (!IS_PARTITIONED_REL(rel))
4312  return;
4313 
4314  /* The relation should have consider_partitionwise_join set. */
4316 
4317  /* Guard against stack overflow due to overly deep partition hierarchy. */
4319 
4320  num_parts = rel->nparts;
4321  part_rels = rel->part_rels;
4322 
4323  /* Collect non-dummy child-joins. */
4324  for (cnt_parts = 0; cnt_parts < num_parts; cnt_parts++)
4325  {
4326  RelOptInfo *child_rel = part_rels[cnt_parts];
4327 
4328  /* If it's been pruned entirely, it's certainly dummy. */
4329  if (child_rel == NULL)
4330  continue;
4331 
4332  /* Make partitionwise join paths for this partitioned child-join. */
4333  generate_partitionwise_join_paths(root, child_rel);
4334 
4335  /* If we failed to make any path for this child, we must give up. */
4336  if (child_rel->pathlist == NIL)
4337  {
4338  /*
4339  * Mark the parent joinrel as unpartitioned so that later
4340  * functions treat it correctly.
4341  */
4342  rel->nparts = 0;
4343  return;
4344  }
4345 
4346  /* Else, identify the cheapest path for it. */
4347  set_cheapest(child_rel);
4348 
4349  /* Dummy children need not be scanned, so ignore those. */
4350  if (IS_DUMMY_REL(child_rel))
4351  continue;
4352 
4353 #ifdef OPTIMIZER_DEBUG
4354  debug_print_rel(root, child_rel);
4355 #endif
4356 
4357  live_children = lappend(live_children, child_rel);
4358  }
4359 
4360  /* If all child-joins are dummy, parent join is also dummy. */
4361  if (!live_children)
4362  {
4363  mark_dummy_rel(rel);
4364  return;
4365  }
4366 
4367  /* Build additional paths for this rel from child-join paths. */
4368  add_paths_to_append_rel(root, rel, live_children);
4369  list_free(live_children);
4370 }
void generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:4299
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1305
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1363
void list_free(List *list)
Definition: list.c:1545
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1914
#define IS_JOIN_REL(rel)
Definition: pathnodes.h:829
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1041
void check_stack_depth(void)
Definition: postgres.c:3523
bool consider_partitionwise_join
Definition: pathnodes.h:978

References add_paths_to_append_rel(), Assert(), check_stack_depth(), RelOptInfo::consider_partitionwise_join, IS_DUMMY_REL, IS_JOIN_REL, IS_PARTITIONED_REL, lappend(), list_free(), mark_dummy_rel(), NIL, RelOptInfo::nparts, RelOptInfo::pathlist, and set_cheapest().

Referenced by merge_clump(), and standard_join_search().

◆ generate_useful_gather_paths()

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

Definition at line 3198 of file allpaths.c.

3199 {
3200  ListCell *lc;
3201  double rows;
3202  double *rowsp = NULL;
3203  List *useful_pathkeys_list = NIL;
3204  Path *cheapest_partial_path = NULL;
3205 
3206  /* If there are no partial paths, there's nothing to do here. */
3207  if (rel->partial_pathlist == NIL)
3208  return;
3209 
3210  /* Should we override the rel's rowcount estimate? */
3211  if (override_rows)
3212  rowsp = &rows;
3213 
3214  /* generate the regular gather (merge) paths */
3215  generate_gather_paths(root, rel, override_rows);
3216 
3217  /* consider incremental sort for interesting orderings */
3218  useful_pathkeys_list = get_useful_pathkeys_for_relation(root, rel, true);
3219 
3220  /* used for explicit (full) sort paths */
3221  cheapest_partial_path = linitial(rel->partial_pathlist);
3222 
3223  /*
3224  * Consider sorted paths for each interesting ordering. We generate both
3225  * incremental and full sort.
3226  */
3227  foreach(lc, useful_pathkeys_list)
3228  {
3229  List *useful_pathkeys = lfirst(lc);
3230  ListCell *lc2;
3231  bool is_sorted;
3232  int presorted_keys;
3233 
3234  foreach(lc2, rel->partial_pathlist)
3235  {
3236  Path *subpath = (Path *) lfirst(lc2);
3237  GatherMergePath *path;
3238 
3239  is_sorted = pathkeys_count_contained_in(useful_pathkeys,
3240  subpath->pathkeys,
3241  &presorted_keys);
3242 
3243  /*
3244  * We don't need to consider the case where a subpath is already
3245  * fully sorted because generate_gather_paths already creates a
3246  * gather merge path for every subpath that has pathkeys present.
3247  *
3248  * But since the subpath is already sorted, we know we don't need
3249  * to consider adding a sort (full or incremental) on top of it,
3250  * so we can continue here.
3251  */
3252  if (is_sorted)
3253  continue;
3254 
3255  /*
3256  * Try at least sorting the cheapest path and also try
3257  * incrementally sorting any path which is partially sorted
3258  * already (no need to deal with paths which have presorted keys
3259  * when incremental sort is disabled unless it's the cheapest
3260  * input path).
3261  */
3262  if (subpath != cheapest_partial_path &&
3263  (presorted_keys == 0 || !enable_incremental_sort))
3264  continue;
3265 
3266  /*
3267  * Consider regular sort for any path that's not presorted or if
3268  * incremental sort is disabled. We've no need to consider both
3269  * sort and incremental sort on the same path. We assume that
3270  * incremental sort is always faster when there are presorted
3271  * keys.
3272  *
3273  * This is not redundant with the gather paths created in
3274  * generate_gather_paths, because that doesn't generate ordered
3275  * output. Here we add an explicit sort to match the useful
3276  * ordering.
3277  */
3278  if (presorted_keys == 0 || !enable_incremental_sort)
3279  {
3280  subpath = (Path *) create_sort_path(root,
3281  rel,
3282  subpath,
3283  useful_pathkeys,
3284  -1.0);
3285  rows = subpath->rows * subpath->parallel_workers;
3286  }
3287  else
3289  rel,
3290  subpath,
3291  useful_pathkeys,
3292  presorted_keys,
3293  -1);
3294  path = create_gather_merge_path(root, rel,
3295  subpath,
3296  rel->reltarget,
3297  subpath->pathkeys,
3298  NULL,
3299  rowsp);
3300 
3301  add_path(rel, &path->path);
3302  }
3303  }
3304 }
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:3060
static List * get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel, bool require_parallel_safe)
Definition: allpaths.c:3130
bool enable_incremental_sort
Definition: costsize.c:141
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition: pathkeys.c:359
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
Definition: pathnode.c:2959
IncrementalSortPath * create_incremental_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, int presorted_keys, double limit_tuples)
Definition: pathnode.c:2910

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

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

◆ get_cheapest_parameterized_child_path()

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

Definition at line 1978 of file allpaths.c.

1980 {
1981  Path *cheapest;
1982  ListCell *lc;
1983 
1984  /*
1985  * Look up the cheapest existing path with no more than the needed
1986  * parameterization. If it has exactly the needed parameterization, we're
1987  * done.
1988  */
1989  cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
1990  NIL,
1991  required_outer,
1992  TOTAL_COST,
1993  false);
1994  Assert(cheapest != NULL);
1995  if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
1996  return cheapest;
1997 
1998  /*
1999  * Otherwise, we can "reparameterize" an existing path to match the given
2000  * parameterization, which effectively means pushing down additional
2001  * joinquals to be checked within the path's scan. However, some existing
2002  * paths might check the available joinquals already while others don't;
2003  * therefore, it's not clear which existing path will be cheapest after
2004  * reparameterization. We have to go through them all and find out.
2005  */
2006  cheapest = NULL;
2007  foreach(lc, rel->pathlist)
2008  {
2009  Path *path = (Path *) lfirst(lc);
2010 
2011  /* Can't use it if it needs more than requested parameterization */
2012  if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
2013  continue;
2014 
2015  /*
2016  * Reparameterization can only increase the path's cost, so if it's
2017  * already more expensive than the current cheapest, forget it.
2018  */
2019  if (cheapest != NULL &&
2020  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2021  continue;
2022 
2023  /* Reparameterize if needed, then recheck cost */
2024  if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
2025  {
2026  path = reparameterize_path(root, path, required_outer, 1.0);
2027  if (path == NULL)
2028  continue; /* failed to reparameterize this one */
2029  Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
2030 
2031  if (cheapest != NULL &&
2032  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2033  continue;
2034  }
2035 
2036  /* We have a new best path */
2037  cheapest = path;
2038  }
2039 
2040  /* Return the best path, or NULL if we found no suitable candidate */
2041  return cheapest;
2042 }
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:363
Path * reparameterize_path(PlannerInfo *root, Path *path, Relids required_outer, double loop_count)
Definition: pathnode.c:3886
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:71

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

◆ get_singleton_append_subpath()

static Path * get_singleton_append_subpath ( Path path)
static

Definition at line 2111 of file allpaths.c.

2112 {
2113  Assert(!path->parallel_aware);
2114 
2115  if (IsA(path, AppendPath))
2116  {
2117  AppendPath *apath = (AppendPath *) path;
2118 
2119  if (list_length(apath->subpaths) == 1)
2120  return (Path *) linitial(apath->subpaths);
2121  }
2122  else if (IsA(path, MergeAppendPath))
2123  {
2124  MergeAppendPath *mpath = (MergeAppendPath *) path;
2125 
2126  if (list_length(mpath->subpaths) == 1)
2127  return (Path *) linitial(mpath->subpaths);
2128  }
2129 
2130  return path;
2131 }

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

Referenced by generate_orderedappend_paths().

◆ get_useful_pathkeys_for_relation()

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

Definition at line 3130 of file allpaths.c.

3132 {
3133  List *useful_pathkeys_list = NIL;
3134 
3135  /*
3136  * Considering query_pathkeys is always worth it, because it might allow
3137  * us to avoid a total sort when we have a partially presorted path
3138  * available or to push the total sort into the parallel portion of the
3139  * query.
3140  */
3141  if (root->query_pathkeys)
3142  {
3143  ListCell *lc;
3144  int npathkeys = 0; /* useful pathkeys */
3145 
3146  foreach(lc, root->query_pathkeys)
3147  {
3148  PathKey *pathkey = (PathKey *) lfirst(lc);
3149  EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
3150 
3151  /*
3152  * We can only build a sort for pathkeys that contain a
3153  * safe-to-compute-early EC member computable from the current
3154  * relation's reltarget, so ignore the remainder of the list as
3155  * soon as we find a pathkey without such a member.
3156  *
3157  * It's still worthwhile to return any prefix of the pathkeys list
3158  * that meets this requirement, as we may be able to do an
3159  * incremental sort.
3160  *
3161  * If requested, ensure the sort expression is parallel-safe too.
3162  */
3163  if (!relation_can_be_sorted_early(root, rel, pathkey_ec,
3164  require_parallel_safe))
3165  break;
3166 
3167  npathkeys++;
3168  }
3169 
3170  /*
3171  * The whole query_pathkeys list matches, so append it directly, to
3172  * allow comparing pathkeys easily by comparing list pointer. If we
3173  * have to truncate the pathkeys, we gotta do a copy though.
3174  */
3175  if (npathkeys == list_length(root->query_pathkeys))
3176  useful_pathkeys_list = lappend(useful_pathkeys_list,
3177  root->query_pathkeys);
3178  else if (npathkeys > 0)
3179  useful_pathkeys_list = lappend(useful_pathkeys_list,
3181  npathkeys));
3182  }
3183 
3184  return useful_pathkeys_list;
3185 }
bool relation_can_be_sorted_early(PlannerInfo *root, RelOptInfo *rel, EquivalenceClass *ec, bool require_parallel_safe)
Definition: equivclass.c:922
List * query_pathkeys
Definition: pathnodes.h:382

References lappend(), lfirst, list_copy_head(), list_length(), NIL, PlannerInfo::query_pathkeys, and relation_can_be_sorted_early().

Referenced by generate_useful_gather_paths().

◆ has_multiple_baserels()

static bool has_multiple_baserels ( PlannerInfo root)
static

Definition at line 2171 of file allpaths.c.

2172 {
2173  int num_base_rels = 0;
2174  Index rti;
2175 
2176  for (rti = 1; rti < root->simple_rel_array_size; rti++)
2177  {
2178  RelOptInfo *brel = root->simple_rel_array[rti];
2179 
2180  if (brel == NULL)
2181  continue;
2182 
2183  /* ignore RTEs that are "other rels" */
2184  if (brel->reloptkind == RELOPT_BASEREL)
2185  if (++num_base_rels > 1)
2186  return true;
2187  }
2188  return false;
2189 }
unsigned int Index
Definition: c.h:603
int simple_rel_array_size
Definition: pathnodes.h:229

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

Referenced by set_subquery_pathlist().

◆ make_one_rel()

RelOptInfo* make_one_rel ( PlannerInfo root,
List joinlist 
)

Definition at line 174 of file allpaths.c.

175 {
176  RelOptInfo *rel;
177  Index rti;
178  double total_pages;
179 
180  /* Mark base rels as to whether we care about fast-start plans */
182 
183  /*
184  * Compute size estimates and consider_parallel flags for each base rel.
185  */
186  set_base_rel_sizes(root);
187 
188  /*
189  * We should now have size estimates for every actual table involved in
190  * the query, and we also know which if any have been deleted from the
191  * query by join removal, pruned by partition pruning, or eliminated by
192  * constraint exclusion. So we can now compute total_table_pages.
193  *
194  * Note that appendrels are not double-counted here, even though we don't
195  * bother to distinguish RelOptInfos for appendrel parents, because the
196  * parents will have pages = 0.
197  *
198  * XXX if a table is self-joined, we will count it once per appearance,
199  * which perhaps is the wrong thing ... but that's not completely clear,
200  * and detecting self-joins here is difficult, so ignore it for now.
201  */
202  total_pages = 0;
203  for (rti = 1; rti < root->simple_rel_array_size; rti++)
204  {
205  RelOptInfo *brel = root->simple_rel_array[rti];
206 
207  /* there may be empty slots corresponding to non-baserel RTEs */
208  if (brel == NULL)
209  continue;
210 
211  Assert(brel->relid == rti); /* sanity check on array */
212 
213  if (IS_DUMMY_REL(brel))
214  continue;
215 
216  if (IS_SIMPLE_REL(brel))
217  total_pages += (double) brel->pages;
218  }
219  root->total_table_pages = total_pages;
220 
221  /*
222  * Generate access paths for each base rel.
223  */
225 
226  /*
227  * Generate access paths for the entire join tree.
228  */
229  rel = make_rel_from_joinlist(root, joinlist);
230 
231  /*
232  * The result should join all and only the query's base + outer-join rels.
233  */
234  Assert(bms_equal(rel->relids, root->all_query_rels));
235 
236  return rel;
237 }
static void set_base_rel_sizes(PlannerInfo *root)
Definition: allpaths.c:293
static void set_base_rel_consider_startup(PlannerInfo *root)
Definition: allpaths.c:250
static void set_base_rel_pathlists(PlannerInfo *root)
Definition: allpaths.c:336
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:3314
Relids all_query_rels
Definition: pathnodes.h:266
Cardinality total_table_pages
Definition: pathnodes.h:475
Relids relids
Definition: pathnodes.h:856
Index relid
Definition: pathnodes.h:903

References PlannerInfo::all_query_rels, Assert(), bms_equal(), IS_DUMMY_REL, IS_SIMPLE_REL, make_rel_from_joinlist(), RelOptInfo::pages, RelOptInfo::relid, RelOptInfo::relids, set_base_rel_consider_startup(), set_base_rel_pathlists(), set_base_rel_sizes(), PlannerInfo::simple_rel_array_size, and PlannerInfo::total_table_pages.

Referenced by query_planner().

◆ make_rel_from_joinlist()

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

Definition at line 3314 of file allpaths.c.

3315 {
3316  int levels_needed;
3317  List *initial_rels;
3318  ListCell *jl;
3319 
3320  /*
3321  * Count the number of child joinlist nodes. This is the depth of the
3322  * dynamic-programming algorithm we must employ to consider all ways of
3323  * joining the child nodes.
3324  */
3325  levels_needed = list_length(joinlist);
3326 
3327  if (levels_needed <= 0)
3328  return NULL; /* nothing to do? */
3329 
3330  /*
3331  * Construct a list of rels corresponding to the child joinlist nodes.
3332  * This may contain both base rels and rels constructed according to
3333  * sub-joinlists.
3334  */
3335  initial_rels = NIL;
3336  foreach(jl, joinlist)
3337  {
3338  Node *jlnode = (Node *) lfirst(jl);
3339  RelOptInfo *thisrel;
3340 
3341  if (IsA(jlnode, RangeTblRef))
3342  {
3343  int varno = ((RangeTblRef *) jlnode)->rtindex;
3344 
3345  thisrel = find_base_rel(root, varno);
3346  }
3347  else if (IsA(jlnode, List))
3348  {
3349  /* Recurse to handle subproblem */
3350  thisrel = make_rel_from_joinlist(root, (List *) jlnode);
3351  }
3352  else
3353  {
3354  elog(ERROR, "unrecognized joinlist node type: %d",
3355  (int) nodeTag(jlnode));
3356  thisrel = NULL; /* keep compiler quiet */
3357  }
3358 
3359  initial_rels = lappend(initial_rels, thisrel);
3360  }
3361 
3362  if (levels_needed == 1)
3363  {
3364  /*
3365  * Single joinlist node, so we're done.
3366  */
3367  return (RelOptInfo *) linitial(initial_rels);
3368  }
3369  else
3370  {
3371  /*
3372  * Consider the different orders in which we could join the rels,
3373  * using a plugin, GEQO, or the regular join search code.
3374  *
3375  * We put the initial_rels list into a PlannerInfo field because
3376  * has_legal_joinclause() needs to look at it (ugly :-().
3377  */
3378  root->initial_rels = initial_rels;
3379 
3380  if (join_search_hook)
3381  return (*join_search_hook) (root, levels_needed, initial_rels);
3382  else if (enable_geqo && levels_needed >= geqo_threshold)
3383  return geqo(root, levels_needed, initial_rels);
3384  else
3385  return standard_join_search(root, levels_needed, initial_rels);
3386  }
3387 }
RelOptInfo * standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
Definition: allpaths.c:3419
int geqo_threshold
Definition: allpaths.c:83
join_search_hook_type join_search_hook
Definition: allpaths.c:91
bool enable_geqo
Definition: allpaths.c:82
RelOptInfo * geqo(PlannerInfo *root, int number_of_rels, List *initial_rels)
Definition: geqo_main.c:67
#define nodeTag(nodeptr)
Definition: nodes.h:133
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:405

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

Referenced by make_one_rel().

◆ qual_is_pushdown_safe()

static pushdown_safe_type qual_is_pushdown_safe ( Query subquery,
Index  rti,
RestrictInfo rinfo,
pushdown_safety_info safetyInfo 
)
static

Definition at line 3863 of file allpaths.c.

3865 {
3867  Node *qual = (Node *) rinfo->clause;
3868  List *vars;
3869  ListCell *vl;
3870 
3871  /* Refuse subselects (point 1) */
3872  if (contain_subplans(qual))
3873  return PUSHDOWN_UNSAFE;
3874 
3875  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
3876  if (safetyInfo->unsafeVolatile &&
3877  contain_volatile_functions((Node *) rinfo))
3878  return PUSHDOWN_UNSAFE;
3879 
3880  /* Refuse leaky quals if told to (point 3) */
3881  if (safetyInfo->unsafeLeaky &&
3882  contain_leaked_vars(qual))
3883  return PUSHDOWN_UNSAFE;
3884 
3885  /*
3886  * Examine all Vars used in clause. Since it's a restriction clause, all
3887  * such Vars must refer to subselect output columns ... unless this is
3888  * part of a LATERAL subquery, in which case there could be lateral
3889  * references.
3890  *
3891  * By omitting the relevant flags, this also gives us a cheap sanity check
3892  * that no aggregates or window functions appear in the qual. Those would
3893  * be unsafe to push down, but at least for the moment we could never see
3894  * any in a qual anyhow.
3895  */
3897  foreach(vl, vars)
3898  {
3899  Var *var = (Var *) lfirst(vl);
3900 
3901  /*
3902  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
3903  * It's not clear whether a PHV could safely be pushed down, and even
3904  * less clear whether such a situation could arise in any cases of
3905  * practical interest anyway. So for the moment, just refuse to push
3906  * down.
3907  */
3908  if (!IsA(var, Var))
3909  {
3910  safe = PUSHDOWN_UNSAFE;
3911  break;
3912  }
3913 
3914  /*
3915  * Punt if we find any lateral references. It would be safe to push
3916  * these down, but we'd have to convert them into outer references,
3917  * which subquery_push_qual lacks the infrastructure to do. The case
3918  * arises so seldom that it doesn't seem worth working hard on.
3919  */
3920  if (var->varno != rti)
3921  {
3922  safe = PUSHDOWN_UNSAFE;
3923  break;
3924  }
3925 
3926  /* Subqueries have no system columns */
3927  Assert(var->varattno >= 0);
3928 
3929  /* Check point 4 */
3930  if (var->varattno == 0)
3931  {
3932  safe = PUSHDOWN_UNSAFE;
3933  break;
3934  }
3935 
3936  /* Check point 5 */
3937  if (safetyInfo->unsafeFlags[var->varattno] != 0)
3938  {
3939  if (safetyInfo->unsafeFlags[var->varattno] &
3942  {
3943  safe = PUSHDOWN_UNSAFE;
3944  break;
3945  }
3946  else
3947  {
3948  /* UNSAFE_NOTIN_PARTITIONBY_CLAUSE is ok for run conditions */
3950  /* don't break, we might find another Var that's unsafe */
3951  }
3952  }
3953  }
3954 
3955  list_free(vars);
3956 
3957  return safe;
3958 }
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1180
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:187
Expr * clause
Definition: pathnodes.h:2529
int varno
Definition: primnodes.h:233
Definition: regcomp.c:281
List * pull_var_clause(Node *node, int flags)
Definition: var.c:607

References Assert(), RestrictInfo::clause, contain_leaked_vars(), contain_subplans(), contain_volatile_functions(), if(), IsA, lfirst, list_free(), pull_var_clause(), PUSHDOWN_SAFE, PUSHDOWN_UNSAFE, PUSHDOWN_WINDOWCLAUSE_RUNCOND, PVC_INCLUDE_PLACEHOLDERS, UNSAFE_HAS_SET_FUNC, UNSAFE_HAS_VOLATILE_FUNC, UNSAFE_NOTIN_DISTINCTON_CLAUSE, UNSAFE_TYPE_MISMATCH, pushdown_safety_info::unsafeFlags, pushdown_safety_info::unsafeLeaky, pushdown_safety_info::unsafeVolatile, Var::varattno, and Var::varno.

Referenced by set_subquery_pathlist().

◆ recurse_push_qual()

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

Definition at line 4011 of file allpaths.c.

4013 {
4014  if (IsA(setOp, RangeTblRef))
4015  {
4016  RangeTblRef *rtr = (RangeTblRef *) setOp;
4017  RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
4018  Query *subquery = subrte->subquery;
4019 
4020  Assert(subquery != NULL);
4021  subquery_push_qual(subquery, rte, rti, qual);
4022  }
4023  else if (IsA(setOp, SetOperationStmt))
4024  {
4025  SetOperationStmt *op = (SetOperationStmt *) setOp;
4026 
4027  recurse_push_qual(op->larg, topquery, rte, rti, qual);
4028  recurse_push_qual(op->rarg, topquery, rte, rti, qual);
4029  }
4030  else
4031  {
4032  elog(ERROR, "unrecognized node type: %d",
4033  (int) nodeTag(setOp));
4034  }
4035 }
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:3964
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:4011
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * rtable
Definition: parsenodes.h:174
Query * subquery
Definition: parsenodes.h:1080

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

◆ recurse_pushdown_safe()

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

Definition at line 3646 of file allpaths.c.

3648 {
3649  if (IsA(setOp, RangeTblRef))
3650  {
3651  RangeTblRef *rtr = (RangeTblRef *) setOp;
3652  RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
3653  Query *subquery = rte->subquery;
3654 
3655  Assert(subquery != NULL);
3656  return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
3657  }
3658  else if (IsA(setOp, SetOperationStmt))
3659  {
3660  SetOperationStmt *op = (SetOperationStmt *) setOp;
3661 
3662  /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
3663  if (op->op == SETOP_EXCEPT)
3664  return false;
3665  /* Else recurse */
3666  if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
3667  return false;
3668  if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
3669  return false;
3670  }
3671  else
3672  {
3673  elog(ERROR, "unrecognized node type: %d",
3674  (int) nodeTag(setOp));
3675  }
3676  return true;
3677 }
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3590
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3646
@ SETOP_EXCEPT
Definition: parsenodes.h:1975
SetOperation op
Definition: parsenodes.h:2050

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

◆ remove_unused_subquery_outputs()

static void remove_unused_subquery_outputs ( Query subquery,
RelOptInfo rel,
Bitmapset extra_used_attrs 
)
static

Definition at line 4063 of file allpaths.c.

4065 {
4066  Bitmapset *attrs_used;
4067  ListCell *lc;
4068 
4069  /*
4070  * Just point directly to extra_used_attrs. No need to bms_copy as none of
4071  * the current callers use the Bitmapset after calling this function.
4072  */
4073  attrs_used = extra_used_attrs;
4074 
4075  /*
4076  * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
4077  * could update all the child SELECTs' tlists, but it seems not worth the
4078  * trouble presently.
4079  */
4080  if (subquery->setOperations)
4081  return;
4082 
4083  /*
4084  * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
4085  * time: all its output columns must be used in the distinctClause.
4086  */
4087  if (subquery->distinctClause && !subquery->hasDistinctOn)
4088  return;
4089 
4090  /*
4091  * Collect a bitmap of all the output column numbers used by the upper
4092  * query.
4093  *
4094  * Add all the attributes needed for joins or final output. Note: we must
4095  * look at rel's targetlist, not the attr_needed data, because attr_needed
4096  * isn't computed for inheritance child rels, cf set_append_rel_size().
4097  * (XXX might be worth changing that sometime.)
4098  */
4099  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
4100 
4101  /* Add all the attributes used by un-pushed-down restriction clauses. */
4102  foreach(lc, rel->baserestrictinfo)
4103  {
4104  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
4105 
4106  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
4107  }
4108 
4109  /*
4110  * If there's a whole-row reference to the subquery, we can't remove
4111  * anything.
4112  */
4114  return;
4115 
4116  /*
4117  * Run through the tlist and zap entries we don't need. It's okay to
4118  * modify the tlist items in-place because set_subquery_pathlist made a
4119  * copy of the subquery.
4120  */
4121  foreach(lc, subquery->targetList)
4122  {
4123  TargetEntry *tle = (TargetEntry *) lfirst(lc);
4124  Node *texpr = (Node *) tle->expr;
4125 
4126  /*
4127  * If it has a sortgroupref number, it's used in some sort/group
4128  * clause so we'd better not remove it. Also, don't remove any
4129  * resjunk columns, since their reason for being has nothing to do
4130  * with anybody reading the subquery's output. (It's likely that
4131  * resjunk columns in a sub-SELECT would always have ressortgroupref
4132  * set, but even if they don't, it seems imprudent to remove them.)
4133  */
4134  if (tle->ressortgroupref || tle->resjunk)
4135  continue;
4136 
4137  /*
4138  * If it's used by the upper query, we can't remove it.
4139  */
4141  attrs_used))
4142  continue;
4143 
4144  /*
4145  * If it contains a set-returning function, we can't remove it since
4146  * that could change the number of rows returned by the subquery.
4147  */
4148  if (subquery->hasTargetSRFs &&
4149  expression_returns_set(texpr))
4150  continue;
4151 
4152  /*
4153  * If it contains volatile functions, we daren't remove it for fear
4154  * that the user is expecting their side-effects to happen.
4155  */
4156  if (contain_volatile_functions(texpr))
4157  continue;
4158 
4159  /*
4160  * OK, we don't need it. Replace the expression with a NULL constant.
4161  * Preserve the exposed type of the expression, in case something
4162  * looks at the rowtype of the subquery's result.
4163  */
4164  tle->expr = (Expr *) makeNullConst(exprType(texpr),
4165  exprTypmod(texpr),
4166  exprCollation(texpr));
4167  }
4168 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:340
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:282
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786
List * exprs
Definition: pathnodes.h:1501
Node * setOperations
Definition: parsenodes.h:216
List * baserestrictinfo
Definition: pathnodes.h:964
Index ressortgroupref
Definition: primnodes.h:1901
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:291

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

Referenced by set_subquery_pathlist().

◆ set_append_rel_pathlist()

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

Definition at line 1235 of file allpaths.c.

1237 {
1238  int parentRTindex = rti;
1239  List *live_childrels = NIL;
1240  ListCell *l;
1241 
1242  /*
1243  * Generate access paths for each member relation, and remember the
1244  * non-dummy children.
1245  */
1246  foreach(l, root->append_rel_list)
1247  {
1248  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1249  int childRTindex;
1250  RangeTblEntry *childRTE;
1251  RelOptInfo *childrel;
1252 
1253  /* append_rel_list contains all append rels; ignore others */
1254  if (appinfo->parent_relid != parentRTindex)
1255  continue;
1256 
1257  /* Re-locate the child RTE and RelOptInfo */
1258  childRTindex = appinfo->child_relid;
1259  childRTE = root->simple_rte_array[childRTindex];
1260  childrel = root->simple_rel_array[childRTindex];
1261 
1262  /*
1263  * If set_append_rel_size() decided the parent appendrel was
1264  * parallel-unsafe at some point after visiting this child rel, we
1265  * need to propagate the unsafety marking down to the child, so that
1266  * we don't generate useless partial paths for it.
1267  */
1268  if (!rel->consider_parallel)
1269  childrel->consider_parallel = false;
1270 
1271  /*
1272  * Compute the child's access paths.
1273  */
1274  set_rel_pathlist(root, childrel, childRTindex, childRTE);
1275 
1276  /*
1277  * If child is dummy, ignore it.
1278  */
1279  if (IS_DUMMY_REL(childrel))
1280  continue;
1281 
1282  /*
1283  * Child is live, so add it to the live_childrels list for use below.
1284  */
1285  live_childrels = lappend(live_childrels, childrel);
1286  }
1287 
1288  /* Add paths to the append relation. */
1289  add_paths_to_append_rel(root, rel, live_childrels);
1290 }
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:472
Index child_relid
Definition: pathnodes.h:2929
Index parent_relid
Definition: pathnodes.h:2928
List * append_rel_list
Definition: pathnodes.h:362

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

Referenced by set_rel_pathlist().

◆ set_append_rel_size()

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

Definition at line 947 of file allpaths.c.

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

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

Referenced by set_rel_size().

◆ set_base_rel_consider_startup()

static void set_base_rel_consider_startup ( PlannerInfo root)
static

Definition at line 250 of file allpaths.c.

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

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

◆ set_base_rel_pathlists()

static void set_base_rel_pathlists ( PlannerInfo root)
static

Definition at line 336 of file allpaths.c.

337 {
338  Index rti;
339 
340  for (rti = 1; rti < root->simple_rel_array_size; rti++)
341  {
342  RelOptInfo *rel = root->simple_rel_array[rti];
343 
344  /* there may be empty slots corresponding to non-baserel RTEs */
345  if (rel == NULL)
346  continue;
347 
348  Assert(rel->relid == rti); /* sanity check on array */
349 
350  /* ignore RTEs that are "other rels" */
351  if (rel->reloptkind != RELOPT_BASEREL)
352  continue;
353 
354  set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
355  }
356 }

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

Referenced by make_one_rel().

◆ set_base_rel_sizes()

static void set_base_rel_sizes ( PlannerInfo root)
static

Definition at line 293 of file allpaths.c.

294 {
295  Index rti;
296 
297  for (rti = 1; rti < root->simple_rel_array_size; rti++)
298  {
299  RelOptInfo *rel = root->simple_rel_array[rti];
300  RangeTblEntry *rte;
301 
302  /* there may be empty slots corresponding to non-baserel RTEs */
303  if (rel == NULL)
304  continue;
305 
306  Assert(rel->relid == rti); /* sanity check on array */
307 
308  /* ignore RTEs that are "other rels" */
309  if (rel->reloptkind != RELOPT_BASEREL)
310  continue;
311 
312  rte = root->simple_rte_array[rti];
313 
314  /*
315  * If parallelism is allowable for this query in general, see whether
316  * it's allowable for this rel in particular. We have to do this
317  * before set_rel_size(), because (a) if this rel is an inheritance
318  * parent, set_append_rel_size() will use and perhaps change the rel's
319  * consider_parallel flag, and (b) for some RTE types, set_rel_size()
320  * goes ahead and makes paths immediately.
321  */
322  if (root->glob->parallelModeOK)
323  set_rel_consider_parallel(root, rel, rte);
324 
325  set_rel_size(root, rel, rti, rte);
326  }
327 }

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

Referenced by make_one_rel().

◆ set_cte_pathlist()

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

Definition at line 2874 of file allpaths.c.

2875 {
2876  Plan *cteplan;
2877  PlannerInfo *cteroot;
2878  Index levelsup;
2879  int ndx;
2880  ListCell *lc;
2881  int plan_id;
2882  Relids required_outer;
2883 
2884  /*
2885  * Find the referenced CTE, and locate the plan previously made for it.
2886  */
2887  levelsup = rte->ctelevelsup;
2888  cteroot = root;
2889  while (levelsup-- > 0)
2890  {
2891  cteroot = cteroot->parent_root;
2892  if (!cteroot) /* shouldn't happen */
2893  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2894  }
2895 
2896  /*
2897  * Note: cte_plan_ids can be shorter than cteList, if we are still working
2898  * on planning the CTEs (ie, this is a side-reference from another CTE).
2899  * So we mustn't use forboth here.
2900  */
2901  ndx = 0;
2902  foreach(lc, cteroot->parse->cteList)
2903  {
2904  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2905 
2906  if (strcmp(cte->ctename, rte->ctename) == 0)
2907  break;
2908  ndx++;
2909  }
2910  if (lc == NULL) /* shouldn't happen */
2911  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2912  if (ndx >= list_length(cteroot->cte_plan_ids))
2913  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2914  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2915  if (plan_id <= 0)
2916  elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
2917  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2918 
2919  /* Mark rel with estimated output rows, width, etc */
2920  set_cte_size_estimates(root, rel, cteplan->plan_rows);
2921 
2922  /*
2923  * We don't support pushing join clauses into the quals of a CTE scan, but
2924  * it could still have required parameterization due to LATERAL refs in
2925  * its tlist.
2926  */
2927  required_outer = rel->lateral_relids;
2928 
2929  /* Generate appropriate path */
2930  add_path(rel, create_ctescan_path(root, rel, required_outer));
2931 }
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:5966
Path * create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2116
static int list_nth_int(const List *list, int n)
Definition: pg_list.h:310
Cardinality plan_rows
Definition: plannodes.h:135
List * subplans
Definition: pathnodes.h:105
List * cte_plan_ids
Definition: pathnodes.h:302
Query * parse
Definition: pathnodes.h:199
List * cteList
Definition: parsenodes.h:172
char * ctename
Definition: parsenodes.h:1163
Index ctelevelsup
Definition: parsenodes.h:1164

References add_path(), 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::parse, Plan::plan_rows, set_cte_size_estimates(), and PlannerGlobal::subplans.

Referenced by set_rel_size().

◆ set_dummy_rel_pathlist()

static void set_dummy_rel_pathlist ( RelOptInfo rel)
static

Definition at line 2145 of file allpaths.c.

2146 {
2147  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
2148  rel->rows = 0;
2149  rel->reltarget->width = 0;
2150 
2151  /* Discard any pre-existing paths; no further need for them */
2152  rel->pathlist = NIL;
2153  rel->partial_pathlist = NIL;
2154 
2155  /* Set up the dummy path */
2156  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
2157  NIL, rel->lateral_relids,
2158  0, false, -1));
2159 
2160  /*
2161  * We set the cheapest-path fields immediately, just in case they were
2162  * pointing at some discarded path. This is redundant when we're called
2163  * from set_rel_size(), but not when called from elsewhere, and doing it
2164  * twice is harmless anyway.
2165  */
2166  set_cheapest(rel);
2167 }

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

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

◆ set_foreign_pathlist()

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

Definition at line 929 of file allpaths.c.

930 {
931  /* Call the FDW's GetForeignPaths function to generate path(s) */
932  rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
933 }

References RangeTblEntry::relid.

Referenced by set_rel_pathlist().

◆ set_foreign_size()

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

Definition at line 905 of file allpaths.c.

906 {
907  /* Mark rel with estimated output rows, width, etc */
908  set_foreign_size_estimates(root, rel);
909 
910  /* Let FDW adjust the size estimates, if it can */
911  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
912 
913  /* ... but do not let it set the rows estimate to zero */
914  rel->rows = clamp_row_est(rel->rows);
915 
916  /*
917  * Also, make sure rel->tuples is not insane relative to rel->rows.
918  * Notably, this ensures sanity if pg_class.reltuples contains -1 and the
919  * FDW doesn't do anything to replace that.
920  */
921  rel->tuples = Max(rel->tuples, rel->rows);
922 }
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:6066
double clamp_row_est(double nrows)
Definition: costsize.c:203

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

Referenced by set_rel_size().

◆ set_function_pathlist()

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

Definition at line 2763 of file allpaths.c.

2764 {
2765  Relids required_outer;
2766  List *pathkeys = NIL;
2767 
2768  /*
2769  * We don't support pushing join clauses into the quals of a function
2770  * scan, but it could still have required parameterization due to LATERAL
2771  * refs in the function expression.
2772  */
2773  required_outer = rel->lateral_relids;
2774 
2775  /*
2776  * The result is considered unordered unless ORDINALITY was used, in which
2777  * case it is ordered by the ordinal column (the last one). See if we
2778  * care, by checking for uses of that Var in equivalence classes.
2779  */
2780  if (rte->funcordinality)
2781  {
2782  AttrNumber ordattno = rel->max_attr;
2783  Var *var = NULL;
2784  ListCell *lc;
2785 
2786  /*
2787  * Is there a Var for it in rel's targetlist? If not, the query did
2788  * not reference the ordinality column, or at least not in any way
2789  * that would be interesting for sorting.
2790  */
2791  foreach(lc, rel->reltarget->exprs)
2792  {
2793  Var *node = (Var *) lfirst(lc);
2794 
2795  /* checking varno/varlevelsup is just paranoia */
2796  if (IsA(node, Var) &&
2797  node->varattno == ordattno &&
2798  node->varno == rel->relid &&
2799  node->varlevelsup == 0)
2800  {
2801  var = node;
2802  break;
2803  }
2804  }
2805 
2806  /*
2807  * Try to build pathkeys for this Var with int8 sorting. We tell
2808  * build_expression_pathkey not to build any new equivalence class; if
2809  * the Var isn't already mentioned in some EC, it means that nothing
2810  * cares about the ordering.
2811  */
2812  if (var)
2813  pathkeys = build_expression_pathkey(root,
2814  (Expr *) var,
2815  Int8LessOperator,
2816  rel->relids,
2817  false);
2818  }
2819 
2820  /* Generate appropriate path */
2821  add_path(rel, create_functionscan_path(root, rel,
2822  pathkeys, required_outer));
2823 }
int16 AttrNumber
Definition: attnum.h:21
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Oid opno, Relids rel, bool create_it)
Definition: pathkeys.c:801
Path * create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer)
Definition: pathnode.c:2038
bool funcordinality
Definition: parsenodes.h:1148
Index varlevelsup
Definition: primnodes.h:258

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

◆ set_namedtuplestore_pathlist()

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

Definition at line 2941 of file allpaths.c.

2943 {
2944  Relids required_outer;
2945 
2946  /* Mark rel with estimated output rows, width, etc */
2948 
2949  /*
2950  * We don't support pushing join clauses into the quals of a tuplestore
2951  * scan, but it could still have required parameterization due to LATERAL
2952  * refs in its tlist.
2953  */
2954  required_outer = rel->lateral_relids;
2955 
2956  /* Generate appropriate path */
2957  add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
2958 
2959  /* Select cheapest path (pretty easy in this case...) */
2960  set_cheapest(rel);
2961 }
void set_namedtuplestore_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:6004
Path * create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2141

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

Referenced by set_rel_size().

◆ set_plain_rel_pathlist()

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

Definition at line 767 of file allpaths.c.

768 {
769  Relids required_outer;
770 
771  /*
772  * We don't support pushing join clauses into the quals of a seqscan, but
773  * it could still have required parameterization due to LATERAL refs in
774  * its tlist.
775  */
776  required_outer = rel->lateral_relids;
777 
778  /* Consider sequential scan */
779  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
780 
781  /* If appropriate, consider parallel sequential scan */
782  if (rel->consider_parallel && required_outer == NULL)
783  create_plain_partial_paths(root, rel);
784 
785  /* Consider index scans */
786  create_index_paths(root, rel);
787 
788  /* Consider TID scans */
789  create_tidscan_paths(root, rel);
790 }
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:797
void create_index_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:235
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:460

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

◆ set_plain_rel_size()

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

Definition at line 575 of file allpaths.c.

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

References check_index_predicates(), and set_baserel_size_estimates().

Referenced by set_rel_size().

◆ set_rel_consider_parallel()

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

Definition at line 592 of file allpaths.c.

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

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

Referenced by set_append_rel_size(), and set_base_rel_sizes().

◆ set_rel_pathlist()

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

Definition at line 472 of file allpaths.c.

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

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

Referenced by set_append_rel_pathlist(), and set_base_rel_pathlists().

◆ set_rel_size()

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

Definition at line 363 of file allpaths.c.

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

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

Referenced by set_append_rel_size(), and set_base_rel_sizes().

◆ set_result_pathlist()

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

Definition at line 2971 of file allpaths.c.

2973 {
2974  Relids required_outer;
2975 
2976  /* Mark rel with estimated output rows, width, etc */
2977  set_result_size_estimates(root, rel);
2978 
2979  /*
2980  * We don't support pushing join clauses into the quals of a Result scan,
2981  * but it could still have required parameterization due to LATERAL refs
2982  * in its tlist.
2983  */
2984  required_outer = rel->lateral_relids;
2985 
2986  /* Generate appropriate path */
2987  add_path(rel, create_resultscan_path(root, rel, required_outer));
2988 
2989  /* Select cheapest path (pretty easy in this case...) */
2990  set_cheapest(rel);
2991 }
void set_result_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:6037
Path * create_resultscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2167

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

Referenced by set_rel_size().

◆ set_subquery_pathlist()

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

Definition at line 2495 of file allpaths.c.

2497 {
2498  Query *parse = root->parse;
2499  Query *subquery = rte->subquery;
2500  bool trivial_pathtarget;
2501  Relids required_outer;
2502  pushdown_safety_info safetyInfo;
2503  double tuple_fraction;
2504  RelOptInfo *sub_final_rel;
2505  Bitmapset *run_cond_attrs = NULL;
2506  ListCell *lc;
2507 
2508  /*
2509  * Must copy the Query so that planning doesn't mess up the RTE contents
2510  * (really really need to fix the planner to not scribble on its input,
2511  * someday ... but see remove_unused_subquery_outputs to start with).
2512  */
2513  subquery = copyObject(subquery);
2514 
2515  /*
2516  * If it's a LATERAL subquery, it might contain some Vars of the current
2517  * query level, requiring it to be treated as parameterized, even though
2518  * we don't support pushing down join quals into subqueries.
2519  */
2520  required_outer = rel->lateral_relids;
2521 
2522  /*
2523  * Zero out result area for subquery_is_pushdown_safe, so that it can set
2524  * flags as needed while recursing. In particular, we need a workspace
2525  * for keeping track of the reasons why columns are unsafe to reference.
2526  * These reasons are stored in the bits inside unsafeFlags[i] when we
2527  * discover reasons that column i of the subquery is unsafe to be used in
2528  * a pushed-down qual.
2529  */
2530  memset(&safetyInfo, 0, sizeof(safetyInfo));
2531  safetyInfo.unsafeFlags = (unsigned char *)
2532  palloc0((list_length(subquery->targetList) + 1) * sizeof(unsigned char));
2533 
2534  /*
2535  * If the subquery has the "security_barrier" flag, it means the subquery
2536  * originated from a view that must enforce row-level security. Then we
2537  * must not push down quals that contain leaky functions. (Ideally this
2538  * would be checked inside subquery_is_pushdown_safe, but since we don't
2539  * currently pass the RTE to that function, we must do it here.)
2540  */
2541  safetyInfo.unsafeLeaky = rte->security_barrier;
2542 
2543  /*
2544  * If there are any restriction clauses that have been attached to the
2545  * subquery relation, consider pushing them down to become WHERE or HAVING
2546  * quals of the subquery itself. This transformation is useful because it
2547  * may allow us to generate a better plan for the subquery than evaluating
2548  * all the subquery output rows and then filtering them.
2549  *
2550  * There are several cases where we cannot push down clauses. Restrictions
2551  * involving the subquery are checked by subquery_is_pushdown_safe().
2552  * Restrictions on individual clauses are checked by
2553  * qual_is_pushdown_safe(). Also, we don't want to push down
2554  * pseudoconstant clauses; better to have the gating node above the
2555  * subquery.
2556  *
2557  * Non-pushed-down clauses will get evaluated as qpquals of the
2558  * SubqueryScan node.
2559  *
2560  * XXX Are there any cases where we want to make a policy decision not to
2561  * push down a pushable qual, because it'd result in a worse plan?
2562  */
2563  if (rel->baserestrictinfo != NIL &&
2564  subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
2565  {
2566  /* OK to consider pushing down individual quals */
2567  List *upperrestrictlist = NIL;
2568  ListCell *l;
2569 
2570  foreach(l, rel->baserestrictinfo)
2571  {
2572  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2573  Node *clause = (Node *) rinfo->clause;
2574 
2575  if (rinfo->pseudoconstant)
2576  {
2577  upperrestrictlist = lappend(upperrestrictlist, rinfo);
2578  continue;
2579  }
2580 
2581  switch (qual_is_pushdown_safe(subquery, rti, rinfo, &safetyInfo))
2582  {
2583  case PUSHDOWN_SAFE:
2584  /* Push it down */
2585  subquery_push_qual(subquery, rte, rti, clause);
2586  break;
2587 
2589 
2590  /*
2591  * Since we can't push the qual down into the subquery,
2592  * check if it happens to reference a window function. If
2593  * so then it might be useful to use for the WindowAgg's
2594  * runCondition.
2595  */
2596  if (!subquery->hasWindowFuncs ||
2597  check_and_push_window_quals(subquery, rte, rti, clause,
2598  &run_cond_attrs))
2599  {
2600  /*
2601  * subquery has no window funcs or the clause is not a
2602  * suitable window run condition qual or it is, but
2603  * the original must also be kept in the upper query.
2604  */
2605  upperrestrictlist = lappend(upperrestrictlist, rinfo);
2606  }
2607  break;
2608 
2609  case PUSHDOWN_UNSAFE:
2610  upperrestrictlist = lappend(upperrestrictlist, rinfo);
2611  break;
2612  }
2613  }
2614  rel->baserestrictinfo = upperrestrictlist;
2615  /* We don't bother recomputing baserestrict_min_security */
2616  }
2617 
2618  pfree(safetyInfo.unsafeFlags);
2619 
2620  /*
2621  * The upper query might not use all the subquery's output columns; if
2622  * not, we can simplify. Pass the attributes that were pushed down into
2623  * WindowAgg run conditions to ensure we don't accidentally think those
2624  * are unused.
2625  */
2626  remove_unused_subquery_outputs(subquery, rel, run_cond_attrs);
2627 
2628  /*
2629  * We can safely pass the outer tuple_fraction down to the subquery if the
2630  * outer level has no joining, aggregation, or sorting to do. Otherwise
2631  * we'd better tell the subquery to plan for full retrieval. (XXX This
2632  * could probably be made more intelligent ...)
2633  */
2634  if (parse->hasAggs ||
2635  parse->groupClause ||
2636  parse->groupingSets ||
2637  root->hasHavingQual ||
2638  parse->distinctClause ||
2639  parse->sortClause ||
2640  has_multiple_baserels(root))
2641  tuple_fraction = 0.0; /* default case */
2642  else
2643  tuple_fraction = root->tuple_fraction;
2644 
2645  /* plan_params should not be in use in current query level */
2646  Assert(root->plan_params == NIL);
2647 
2648  /* Generate a subroot and Paths for the subquery */
2649  rel->subroot = subquery_planner(root->glob, subquery,
2650  root,
2651  false, tuple_fraction);
2652 
2653  /* Isolate the params needed by this specific subplan */
2654  rel->subplan_params = root->plan_params;
2655  root->plan_params = NIL;
2656 
2657  /*
2658  * It's possible that constraint exclusion proved the subquery empty. If
2659  * so, it's desirable to produce an unadorned dummy path so that we will
2660  * recognize appropriate optimizations at this query level.
2661  */
2662  sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
2663 
2664  if (IS_DUMMY_REL(sub_final_rel))
2665  {
2667  return;
2668  }
2669 
2670  /*
2671  * Mark rel with estimated output rows, width, etc. Note that we have to
2672  * do this before generating outer-query paths, else cost_subqueryscan is
2673  * not happy.
2674  */
2675  set_subquery_size_estimates(root, rel);
2676 
2677  /*
2678  * Also detect whether the reltarget is trivial, so that we can pass that
2679  * info to cost_subqueryscan (rather than re-deriving it multiple times).
2680  * It's trivial if it fetches all the subplan output columns in order.
2681  */
2682  if (list_length(rel->reltarget->exprs) != list_length(subquery->targetList))
2683  trivial_pathtarget = false;
2684  else
2685  {
2686  trivial_pathtarget = true;
2687  foreach(lc, rel->reltarget->exprs)
2688  {
2689  Node *node = (Node *) lfirst(lc);
2690  Var *var;
2691 
2692  if (!IsA(node, Var))
2693  {
2694  trivial_pathtarget = false;
2695  break;
2696  }
2697  var = (Var *) node;
2698  if (var->varno != rti ||
2699  var->varattno != foreach_current_index(lc) + 1)
2700  {
2701  trivial_pathtarget = false;
2702  break;
2703  }
2704  }
2705  }
2706 
2707  /*
2708  * For each Path that subquery_planner produced, make a SubqueryScanPath
2709  * in the outer query.
2710  */
2711  foreach(lc, sub_final_rel->pathlist)
2712  {
2713  Path *subpath = (Path *) lfirst(lc);
2714  List *pathkeys;
2715 
2716  /* Convert subpath's pathkeys to outer representation */
2717  pathkeys = convert_subquery_pathkeys(root,
2718  rel,
2719  subpath->pathkeys,
2720  make_tlist_from_pathtarget(subpath->pathtarget));
2721 
2722  /* Generate outer path using this subpath */
2723  add_path(rel, (Path *)
2724  create_subqueryscan_path(root, rel, subpath,
2725  trivial_pathtarget,
2726  pathkeys, required_outer));
2727  }
2728 
2729  /* If outer rel allows parallelism, do same for partial paths. */
2730  if (rel->consider_parallel && bms_is_empty(required_outer))
2731  {
2732  /* If consider_parallel is false, there should be no partial paths. */
2733  Assert(sub_final_rel->consider_parallel ||
2734  sub_final_rel->partial_pathlist == NIL);
2735 
2736  /* Same for partial paths. */
2737  foreach(lc, sub_final_rel->partial_pathlist)
2738  {
2739  Path *subpath = (Path *) lfirst(lc);
2740  List *pathkeys;
2741 
2742  /* Convert subpath's pathkeys to outer representation */
2743  pathkeys = convert_subquery_pathkeys(root,
2744  rel,
2745  subpath->pathkeys,
2746  make_tlist_from_pathtarget(subpath->pathtarget));
2747 
2748  /* Generate outer path using this subpath */
2749  add_partial_path(rel, (Path *)
2750  create_subqueryscan_path(root, rel, subpath,
2751  trivial_pathtarget,
2752  pathkeys,
2753  required_outer));
2754  }
2755  }
2756 }
static pushdown_safe_type qual_is_pushdown_safe(Query *subquery, Index rti, RestrictInfo *rinfo, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3863
static bool check_and_push_window_quals(Query *subquery, RangeTblEntry *rte, Index rti, Node *clause, Bitmapset **run_cond_attrs)
Definition: allpaths.c:2420
static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel, Bitmapset *extra_used_attrs)
Definition: allpaths.c:4063
static bool has_multiple_baserels(PlannerInfo *root)
Definition: allpaths.c:2171
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5794
#define copyObject(obj)
Definition: nodes.h:244
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
Definition: pathkeys.c:855
SubqueryScanPath * create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, bool trivial_pathtarget, List *pathkeys, Relids required_outer)
Definition: pathnode.c:2008
@ UPPERREL_FINAL
Definition: pathnodes.h:79
#define foreach_current_index(cell)
Definition: pg_list.h:403
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:623
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1443
bool hasHavingQual
Definition: pathnodes.h:493
List * plan_params
Definition: pathnodes.h:217
bool security_barrier
Definition: parsenodes.h:1081
List * subplan_params
Definition: pathnodes.h:933
PlannerInfo * subroot
Definition: pathnodes.h:932
List * make_tlist_from_pathtarget(PathTarget *target)
Definition: tlist.c:624

References add_partial_path(), add_path(), Assert(), RelOptInfo::baserestrictinfo, bms_is_empty, check_and_push_window_quals(), RestrictInfo::clause, RelOptInfo::consider_parallel, convert_subquery_pathkeys(), copyObject, create_subqueryscan_path(), PathTarget::exprs, fetch_upper_rel(), foreach_current_index, PlannerInfo::glob, has_multiple_baserels(), PlannerInfo::hasHavingQual, if(), IS_DUMMY_REL, IsA, lappend(), RelOptInfo::lateral_relids, lfirst, list_length(), make_tlist_from_pathtarget(), NIL, palloc0(), parse(), PlannerInfo::parse, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, pfree(), PlannerInfo::plan_params, PUSHDOWN_SAFE, PUSHDOWN_UNSAFE, PUSHDOWN_WINDOWCLAUSE_RUNCOND, qual_is_pushdown_safe(), RelOptInfo::reltarget, remove_unused_subquery_outputs(), RangeTblEntry::security_barrier, set_dummy_rel_pathlist(), set_subquery_size_estimates(), 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::unsafeFlags, pushdown_safety_info::unsafeLeaky, UPPERREL_FINAL, Var::varattno, and Var::varno.

Referenced by set_rel_size().

◆ set_tablefunc_pathlist()

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

Definition at line 2850 of file allpaths.c.

2851 {
2852  Relids required_outer;
2853 
2854  /*
2855  * We don't support pushing join clauses into the quals of a tablefunc
2856  * scan, but it could still have required parameterization due to LATERAL
2857  * refs in the function expression.
2858  */
2859  required_outer = rel->lateral_relids;
2860 
2861  /* Generate appropriate path */
2862  add_path(rel, create_tablefuncscan_path(root, rel,
2863  required_outer));
2864 }
Path * create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2064

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

Referenced by set_rel_pathlist().

◆ set_tablesample_rel_pathlist()

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

Definition at line 857 of file allpaths.c.

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

References add_path(), PlannerInfo::all_query_rels, 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().

◆ set_tablesample_rel_size()

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

Definition at line 817 of file allpaths.c.

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

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

◆ set_values_pathlist()

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

Definition at line 2830 of file allpaths.c.

2831 {
2832  Relids required_outer;
2833 
2834  /*
2835  * We don't support pushing join clauses into the quals of a values scan,
2836  * but it could still have required parameterization due to LATERAL refs
2837  * in the values expressions.
2838  */
2839  required_outer = rel->lateral_relids;
2840 
2841  /* Generate appropriate path */
2842  add_path(rel, create_valuesscan_path(root, rel, required_outer));
2843 }
Path * create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2090

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

Referenced by set_rel_pathlist().

◆ set_worktable_pathlist()

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

Definition at line 3001 of file allpaths.c.

3002 {
3003  Path *ctepath;
3004  PlannerInfo *cteroot;
3005  Index levelsup;
3006  Relids required_outer;
3007 
3008  /*
3009  * We need to find the non-recursive term's path, which is in the plan
3010  * level that's processing the recursive UNION, which is one level *below*
3011  * where the CTE comes from.
3012  */
3013  levelsup = rte->ctelevelsup;
3014  if (levelsup == 0) /* shouldn't happen */
3015  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3016  levelsup--;
3017  cteroot = root;
3018  while (levelsup-- > 0)
3019  {
3020  cteroot = cteroot->parent_root;
3021  if (!cteroot) /* shouldn't happen */
3022  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
3023  }
3024  ctepath = cteroot->non_recursive_path;
3025  if (!ctepath) /* shouldn't happen */
3026  elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
3027 
3028  /* Mark rel with estimated output rows, width, etc */
3029  set_cte_size_estimates(root, rel, ctepath->rows);
3030 
3031  /*
3032  * We don't support pushing join clauses into the quals of a worktable
3033  * scan, but it could still have required parameterization due to LATERAL
3034  * refs in its tlist. (I'm not sure this is actually possible given the
3035  * restrictions on recursive references, but it's easy enough to support.)
3036  */
3037  required_outer = rel->lateral_relids;
3038 
3039  /* Generate appropriate path */
3040  add_path(rel, create_worktablescan_path(root, rel, required_outer));
3041 }
Path * create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:2193
struct Path * non_recursive_path
Definition: pathnodes.h:523

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

Referenced by set_rel_size().

◆ standard_join_search()

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

Definition at line 3419 of file allpaths.c.

3420 {
3421  int lev;
3422  RelOptInfo *rel;
3423 
3424  /*
3425  * This function cannot be invoked recursively within any one planning
3426  * problem, so join_rel_level[] can't be in use already.
3427  */
3428  Assert(root->join_rel_level == NULL);
3429 
3430  /*
3431  * We employ a simple "dynamic programming" algorithm: we first find all
3432  * ways to build joins of two jointree items, then all ways to build joins
3433  * of three items (from two-item joins and single items), then four-item
3434  * joins, and so on until we have considered all ways to join all the
3435  * items into one rel.
3436  *
3437  * root->join_rel_level[j] is a list of all the j-item rels. Initially we
3438  * set root->join_rel_level[1] to represent all the single-jointree-item
3439  * relations.
3440  */
3441  root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
3442 
3443  root->join_rel_level[1] = initial_rels;
3444 
3445  for (lev = 2; lev <= levels_needed; lev++)
3446  {
3447  ListCell *lc;
3448 
3449  /*
3450  * Determine all possible pairs of relations to be joined at this
3451  * level, and build paths for making each one from every available
3452  * pair of lower-level relations.
3453  */
3454  join_search_one_level(root, lev);
3455 
3456  /*
3457  * Run generate_partitionwise_join_paths() and
3458  * generate_useful_gather_paths() for each just-processed joinrel. We
3459  * could not do this earlier because both regular and partial paths
3460  * can get added to a particular joinrel at multiple times within
3461  * join_search_one_level.
3462  *
3463  * After that, we're done creating paths for the joinrel, so run
3464  * set_cheapest().
3465  */
3466  foreach(lc, root->join_rel_level[lev])
3467  {
3468  rel = (RelOptInfo *) lfirst(lc);
3469 
3470  /* Create paths for partitionwise joins. */
3472 
3473  /*
3474  * Except for the topmost scan/join rel, consider gathering
3475  * partial paths. We'll do the same for the topmost scan/join rel
3476  * once we know the final targetlist (see grouping_planner's and
3477  * its call to apply_scanjoin_target_to_paths).
3478  */
3479  if (!bms_equal(rel->relids, root->all_query_rels))
3480  generate_useful_gather_paths(root, rel, false);
3481 
3482  /* Find and save the cheapest paths for this rel */
3483  set_cheapest(rel);
3484 
3485 #ifdef OPTIMIZER_DEBUG
3486  debug_print_rel(root, rel);
3487 #endif
3488  }
3489  }
3490 
3491  /*
3492  * We should have a single rel at the final level.
3493  */
3494  if (root->join_rel_level[levels_needed] == NIL)
3495  elog(ERROR, "failed to build any %d-way joins", levels_needed);
3496  Assert(list_length(root->join_rel_level[levels_needed]) == 1);
3497 
3498  rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
3499 
3500  root->join_rel_level = NULL;
3501 
3502  return rel;
3503 }
void join_search_one_level(PlannerInfo *root, int level)
Definition: joinrels.c:71

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

Referenced by make_rel_from_joinlist().

◆ subquery_is_pushdown_safe()

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

Definition at line 3590 of file allpaths.c.

3592 {
3593  SetOperationStmt *topop;
3594 
3595  /* Check point 1 */
3596  if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
3597  return false;
3598 
3599  /* Check point 6 */
3600  if (subquery->groupClause && subquery->groupingSets)
3601  return false;
3602 
3603  /* Check points 3, 4, and 5 */
3604  if (subquery->distinctClause ||
3605  subquery->hasWindowFuncs ||
3606  subquery->hasTargetSRFs)
3607  safetyInfo->unsafeVolatile = true;
3608 
3609  /*
3610  * If we're at a leaf query, check for unsafe expressions in its target
3611  * list, and mark any reasons why they're unsafe in unsafeFlags[].
3612  * (Non-leaf nodes in setop trees have only simple Vars in their tlists,
3613  * so no need to check them.)
3614  */
3615  if (subquery->setOperations == NULL)
3616  check_output_expressions(subquery, safetyInfo);
3617 
3618  /* Are we at top level, or looking at a setop component? */
3619  if (subquery == topquery)
3620  {
3621  /* Top level, so check any component queries */
3622  if (subquery->setOperations != NULL)
3623  if (!recurse_pushdown_safe(subquery->setOperations, topquery,
3624  safetyInfo))
3625  return false;
3626  }
3627  else
3628  {
3629  /* Setop component must not have more components (too weird) */
3630  if (subquery->setOperations != NULL)
3631  return false;
3632  /* Check whether setop component output types match top level */
3633  topop = castNode(SetOperationStmt, topquery->setOperations);
3634  Assert(topop);
3636  topop->colTypes,
3637  safetyInfo);
3638  }
3639  return true;
3640 }
static void compare_tlist_datatypes(List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3787
static void check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:3715
Node * limitCount
Definition: parsenodes.h:211
List * groupClause
Definition: parsenodes.h:197
Node * limitOffset
Definition: parsenodes.h:210
List * groupingSets
Definition: parsenodes.h:200

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

◆ subquery_push_qual()

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

Definition at line 3964 of file allpaths.c.

3965 {
3966  if (subquery->setOperations != NULL)
3967  {
3968  /* Recurse to push it separately to each component query */
3969  recurse_push_qual(subquery->setOperations, subquery,
3970  rte, rti, qual);
3971  }
3972  else
3973  {
3974  /*
3975  * We need to replace Vars in the qual (which must refer to outputs of
3976  * the subquery) with copies of the subquery's targetlist expressions.
3977  * Note that at this point, any uplevel Vars in the qual should have
3978  * been replaced with Params, so they need no work.
3979  *
3980  * This step also ensures that when we are pushing into a setop tree,
3981  * each component query gets its own copy of the qual.
3982  */
3983  qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
3984  subquery->targetList,
3986  &subquery->hasSubLinks);
3987 
3988  /*
3989  * Now attach the qual to the proper place: normally WHERE, but if the
3990  * subquery uses grouping or aggregation, put it in HAVING (since the
3991  * qual really refers to the group-result rows).
3992  */
3993  if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
3994  subquery->havingQual = make_and_qual(subquery->havingQual, qual);
3995  else
3996  subquery->jointree->quals =
3997  make_and_qual(subquery->jointree->quals, qual);
3998 
3999  /*
4000  * We need not change the subquery's hasAggs or hasSubLinks flags,
4001  * since we can't be pushing down any aggregates that weren't there
4002  * before, and we don't push down subselects at all.
4003  */
4004  }
4005 }
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: makefuncs.c:692
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:38
Node * quals
Definition: primnodes.h:2014
FromExpr * jointree
Definition: parsenodes.h:181
Node * havingQual
Definition: parsenodes.h:202

References Query::groupClause, Query::groupingSets, 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().

◆ targetIsInAllPartitionLists()

static bool targetIsInAllPartitionLists ( TargetEntry tle,
Query query 
)
static

Definition at line 3820 of file allpaths.c.

3821 {
3822  ListCell *lc;
3823 
3824  foreach(lc, query->windowClause)
3825  {
3826  WindowClause *wc = (WindowClause *) lfirst(lc);
3827 
3829  return false;
3830  }
3831  return true;
3832 }
List * partitionClause
Definition: parsenodes.h:1496

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

Referenced by check_output_expressions().

Variable Documentation

◆ enable_geqo

bool enable_geqo = false

Definition at line 82 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ geqo_threshold

int geqo_threshold

Definition at line 83 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ join_search_hook

join_search_hook_type join_search_hook = NULL

Definition at line 91 of file allpaths.c.

Referenced by make_rel_from_joinlist().

◆ min_parallel_index_scan_size

int min_parallel_index_scan_size

Definition at line 85 of file allpaths.c.

Referenced by compute_parallel_worker(), and parallel_vacuum_compute_workers().

◆ min_parallel_table_scan_size

int min_parallel_table_scan_size

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

Referenced by set_rel_pathlist().