PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/planner.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parse_clause.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "utils/lsyscache.h"
Include dependency graph for allpaths.c:

Go to the source code of this file.

Data Structures

struct  pushdown_safety_info
 

Typedefs

typedef struct pushdown_safety_info pushdown_safety_info
 

Functions

static void set_base_rel_consider_startup (PlannerInfo *root)
 
static void set_base_rel_sizes (PlannerInfo *root)
 
static void set_base_rel_pathlists (PlannerInfo *root)
 
static void set_rel_size (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_plain_rel_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void create_plain_partial_paths (PlannerInfo *root, RelOptInfo *rel)
 
static void set_rel_consider_parallel (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_plain_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablesample_rel_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablesample_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_foreign_size (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_foreign_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_append_rel_size (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_append_rel_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void generate_mergeappend_paths (PlannerInfo *root, RelOptInfo *rel, List *live_childrels, List *all_child_pathkeys, List *partitioned_rels)
 
static Pathget_cheapest_parameterized_child_path (PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
 
static Listaccumulate_append_subpath (List *subpaths, Path *path)
 
static void set_subquery_pathlist (PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
 
static void set_function_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_values_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_tablefunc_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_cte_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_namedtuplestore_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void set_worktable_pathlist (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static RelOptInfomake_rel_from_joinlist (PlannerInfo *root, List *joinlist)
 
static bool subquery_is_pushdown_safe (Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
 
static bool recurse_pushdown_safe (Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
 
static void check_output_expressions (Query *subquery, pushdown_safety_info *safetyInfo)
 
static void compare_tlist_datatypes (List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
 
static bool targetIsInAllPartitionLists (TargetEntry *tle, Query *query)
 
static bool qual_is_pushdown_safe (Query *subquery, Index rti, Node *qual, pushdown_safety_info *safetyInfo)
 
static void subquery_push_qual (Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
 
static void recurse_push_qual (Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
 
static void remove_unused_subquery_outputs (Query *subquery, RelOptInfo *rel)
 
static void add_paths_to_append_rel (PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
 
RelOptInfomake_one_rel (PlannerInfo *root, List *joinlist)
 
void set_dummy_rel_pathlist (RelOptInfo *rel)
 
static bool has_multiple_baserels (PlannerInfo *root)
 
void generate_gather_paths (PlannerInfo *root, RelOptInfo *rel)
 
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)
 

Variables

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

Typedef Documentation

Function Documentation

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

Definition at line 1676 of file allpaths.c.

References IsA, lappend(), list_concat(), list_copy(), AppendPath::subpaths, and MergeAppendPath::subpaths.

Referenced by add_paths_to_append_rel(), and generate_mergeappend_paths().

1677 {
1678  if (IsA(path, AppendPath))
1679  {
1680  AppendPath *apath = (AppendPath *) path;
1681 
1682  /* list_copy is important here to avoid sharing list substructure */
1683  return list_concat(subpaths, list_copy(apath->subpaths));
1684  }
1685  else if (IsA(path, MergeAppendPath))
1686  {
1687  MergeAppendPath *mpath = (MergeAppendPath *) path;
1688 
1689  /* list_copy is important here to avoid sharing list substructure */
1690  return list_concat(subpaths, list_copy(mpath->subpaths));
1691  }
1692  else
1693  return lappend(subpaths, path);
1694 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * subpaths
Definition: relation.h:1180
List * lappend(List *list, void *datum)
Definition: list.c:128
List * subpaths
Definition: relation.h:1200
static void add_paths_to_append_rel ( PlannerInfo root,
RelOptInfo rel,
List live_childrels 
)
static

Definition at line 1278 of file allpaths.c.

References accumulate_append_subpath(), add_partial_path(), add_path(), Assert, bms_equal(), RelOptInfo::cheapest_total_path, compare_pathkeys(), create_append_path(), generate_mergeappend_paths(), get_cheapest_parameterized_child_path(), get_partitioned_child_rels(), lappend(), lfirst, linitial, list_length(), Max, NIL, NULL, Path::parallel_workers, Path::param_info, RelOptInfo::partial_pathlist, PATH_REQ_OUTER, Path::pathkeys, PATHKEYS_EQUAL, RelOptInfo::pathlist, planner_rt_fetch, RelOptInfo::relid, RangeTblEntry::relkind, RELKIND_PARTITIONED_TABLE, and subpath().

Referenced by set_append_rel_pathlist().

1280 {
1281  List *subpaths = NIL;
1282  bool subpaths_valid = true;
1283  List *partial_subpaths = NIL;
1284  bool partial_subpaths_valid = true;
1285  List *all_child_pathkeys = NIL;
1286  List *all_child_outers = NIL;
1287  ListCell *l;
1288  List *partitioned_rels = NIL;
1289  RangeTblEntry *rte;
1290 
1291  rte = planner_rt_fetch(rel->relid, root);
1292  if (rte->relkind == RELKIND_PARTITIONED_TABLE)
1293  {
1294  partitioned_rels = get_partitioned_child_rels(root, rel->relid);
1295  /* The root partitioned table is included as a child rel */
1296  Assert(list_length(partitioned_rels) >= 1);
1297  }
1298 
1299  /*
1300  * For every non-dummy child, remember the cheapest path. Also, identify
1301  * all pathkeys (orderings) and parameterizations (required_outer sets)
1302  * available for the non-dummy member relations.
1303  */
1304  foreach(l, live_childrels)
1305  {
1306  RelOptInfo *childrel = lfirst(l);
1307  ListCell *lcp;
1308 
1309  /*
1310  * If child has an unparameterized cheapest-total path, add that to
1311  * the unparameterized Append path we are constructing for the parent.
1312  * If not, there's no workable unparameterized path.
1313  */
1314  if (childrel->cheapest_total_path->param_info == NULL)
1315  subpaths = accumulate_append_subpath(subpaths,
1316  childrel->cheapest_total_path);
1317  else
1318  subpaths_valid = false;
1319 
1320  /* Same idea, but for a partial plan. */
1321  if (childrel->partial_pathlist != NIL)
1322  partial_subpaths = accumulate_append_subpath(partial_subpaths,
1323  linitial(childrel->partial_pathlist));
1324  else
1325  partial_subpaths_valid = false;
1326 
1327  /*
1328  * Collect lists of all the available path orderings and
1329  * parameterizations for all the children. We use these as a
1330  * heuristic to indicate which sort orderings and parameterizations we
1331  * should build Append and MergeAppend paths for.
1332  */
1333  foreach(lcp, childrel->pathlist)
1334  {
1335  Path *childpath = (Path *) lfirst(lcp);
1336  List *childkeys = childpath->pathkeys;
1337  Relids childouter = PATH_REQ_OUTER(childpath);
1338 
1339  /* Unsorted paths don't contribute to pathkey list */
1340  if (childkeys != NIL)
1341  {
1342  ListCell *lpk;
1343  bool found = false;
1344 
1345  /* Have we already seen this ordering? */
1346  foreach(lpk, all_child_pathkeys)
1347  {
1348  List *existing_pathkeys = (List *) lfirst(lpk);
1349 
1350  if (compare_pathkeys(existing_pathkeys,
1351  childkeys) == PATHKEYS_EQUAL)
1352  {
1353  found = true;
1354  break;
1355  }
1356  }
1357  if (!found)
1358  {
1359  /* No, so add it to all_child_pathkeys */
1360  all_child_pathkeys = lappend(all_child_pathkeys,
1361  childkeys);
1362  }
1363  }
1364 
1365  /* Unparameterized paths don't contribute to param-set list */
1366  if (childouter)
1367  {
1368  ListCell *lco;
1369  bool found = false;
1370 
1371  /* Have we already seen this param set? */
1372  foreach(lco, all_child_outers)
1373  {
1374  Relids existing_outers = (Relids) lfirst(lco);
1375 
1376  if (bms_equal(existing_outers, childouter))
1377  {
1378  found = true;
1379  break;
1380  }
1381  }
1382  if (!found)
1383  {
1384  /* No, so add it to all_child_outers */
1385  all_child_outers = lappend(all_child_outers,
1386  childouter);
1387  }
1388  }
1389  }
1390  }
1391 
1392  /*
1393  * If we found unparameterized paths for all children, build an unordered,
1394  * unparameterized Append path for the rel. (Note: this is correct even
1395  * if we have zero or one live subpath due to constraint exclusion.)
1396  */
1397  if (subpaths_valid)
1398  add_path(rel, (Path *) create_append_path(rel, subpaths, NULL, 0,
1399  partitioned_rels));
1400 
1401  /*
1402  * Consider an append of partial unordered, unparameterized partial paths.
1403  */
1404  if (partial_subpaths_valid)
1405  {
1406  AppendPath *appendpath;
1407  ListCell *lc;
1408  int parallel_workers = 0;
1409 
1410  /*
1411  * Decide on the number of workers to request for this append path.
1412  * For now, we just use the maximum value from among the members. It
1413  * might be useful to use a higher number if the Append node were
1414  * smart enough to spread out the workers, but it currently isn't.
1415  */
1416  foreach(lc, partial_subpaths)
1417  {
1418  Path *path = lfirst(lc);
1419 
1420  parallel_workers = Max(parallel_workers, path->parallel_workers);
1421  }
1422  Assert(parallel_workers > 0);
1423 
1424  /* Generate a partial append path. */
1425  appendpath = create_append_path(rel, partial_subpaths, NULL,
1426  parallel_workers, partitioned_rels);
1427  add_partial_path(rel, (Path *) appendpath);
1428  }
1429 
1430  /*
1431  * Also build unparameterized MergeAppend paths based on the collected
1432  * list of child pathkeys.
1433  */
1434  if (subpaths_valid)
1435  generate_mergeappend_paths(root, rel, live_childrels,
1436  all_child_pathkeys,
1437  partitioned_rels);
1438 
1439  /*
1440  * Build Append paths for each parameterization seen among the child rels.
1441  * (This may look pretty expensive, but in most cases of practical
1442  * interest, the child rels will expose mostly the same parameterizations,
1443  * so that not that many cases actually get considered here.)
1444  *
1445  * The Append node itself cannot enforce quals, so all qual checking must
1446  * be done in the child paths. This means that to have a parameterized
1447  * Append path, we must have the exact same parameterization for each
1448  * child path; otherwise some children might be failing to check the
1449  * moved-down quals. To make them match up, we can try to increase the
1450  * parameterization of lesser-parameterized paths.
1451  */
1452  foreach(l, all_child_outers)
1453  {
1454  Relids required_outer = (Relids) lfirst(l);
1455  ListCell *lcr;
1456 
1457  /* Select the child paths for an Append with this parameterization */
1458  subpaths = NIL;
1459  subpaths_valid = true;
1460  foreach(lcr, live_childrels)
1461  {
1462  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1463  Path *subpath;
1464 
1466  childrel,
1467  required_outer);
1468  if (subpath == NULL)
1469  {
1470  /* failed to make a suitable path for this child */
1471  subpaths_valid = false;
1472  break;
1473  }
1474  subpaths = accumulate_append_subpath(subpaths, subpath);
1475  }
1476 
1477  if (subpaths_valid)
1478  add_path(rel, (Path *)
1479  create_append_path(rel, subpaths, required_outer, 0,
1480  partitioned_rels));
1481  }
1482 }
#define NIL
Definition: pg_list.h:69
static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel, List *live_childrels, List *all_child_pathkeys, List *partitioned_rels)
Definition: allpaths.c:1508
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
static Path * get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: allpaths.c:1595
List * get_partitioned_child_rels(PlannerInfo *root, Index rti)
Definition: planner.c:6076
int parallel_workers
Definition: relation.h:961
ParamPathInfo * param_info
Definition: relation.h:957
List * partial_pathlist
Definition: relation.h:541
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:278
AppendPath * create_append_path(RelOptInfo *rel, List *subpaths, Relids required_outer, int parallel_workers, List *partitioned_rels)
Definition: pathnode.c:1203
#define linitial(l)
Definition: pg_list.h:111
#define planner_rt_fetch(rti, root)
Definition: relation.h:325
struct Path * cheapest_total_path
Definition: relation.h:543
Index relid
Definition: relation.h:553
Bitmapset * Relids
Definition: relation.h:28
List * lappend(List *list, void *datum)
Definition: list.c:128
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
List * pathkeys
Definition: relation.h:968
#define Max(x, y)
Definition: c.h:801
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:973
static List * accumulate_append_subpath(List *subpaths, Path *path)
Definition: allpaths.c:1676
static int list_length(const List *l)
Definition: pg_list.h:89
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
List * pathlist
Definition: relation.h:539
Definition: pg_list.h:45
Definition: relation.h:948
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
static void check_output_expressions ( Query subquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 2618 of file allpaths.c.

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

Referenced by subquery_is_pushdown_safe().

2619 {
2620  ListCell *lc;
2621 
2622  foreach(lc, subquery->targetList)
2623  {
2624  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2625 
2626  if (tle->resjunk)
2627  continue; /* ignore resjunk columns */
2628 
2629  /* We need not check further if output col is already known unsafe */
2630  if (safetyInfo->unsafeColumns[tle->resno])
2631  continue;
2632 
2633  /* Functions returning sets are unsafe (point 1) */
2634  if (subquery->hasTargetSRFs &&
2635  expression_returns_set((Node *) tle->expr))
2636  {
2637  safetyInfo->unsafeColumns[tle->resno] = true;
2638  continue;
2639  }
2640 
2641  /* Volatile functions are unsafe (point 2) */
2642  if (contain_volatile_functions((Node *) tle->expr))
2643  {
2644  safetyInfo->unsafeColumns[tle->resno] = true;
2645  continue;
2646  }
2647 
2648  /* If subquery uses DISTINCT ON, check point 3 */
2649  if (subquery->hasDistinctOn &&
2650  !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
2651  {
2652  /* non-DISTINCT column, so mark it unsafe */
2653  safetyInfo->unsafeColumns[tle->resno] = true;
2654  continue;
2655  }
2656 
2657  /* If subquery uses window functions, check point 4 */
2658  if (subquery->hasWindowFuncs &&
2659  !targetIsInAllPartitionLists(tle, subquery))
2660  {
2661  /* not present in all PARTITION BY clauses, so mark it unsafe */
2662  safetyInfo->unsafeColumns[tle->resno] = true;
2663  continue;
2664  }
2665  }
2666 }
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:509
bool * unsafeColumns
Definition: allpaths.c:52
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:957
bool hasDistinctOn
Definition: parsenodes.h:127
List * targetList
Definition: parsenodes.h:138
bool resjunk
Definition: primnodes.h:1375
List * distinctClause
Definition: parsenodes.h:154
static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
Definition: allpaths.c:2719
AttrNumber resno
Definition: primnodes.h:1369
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
bool hasTargetSRFs
Definition: parsenodes.h:125
#define lfirst(lc)
Definition: pg_list.h:106
bool hasWindowFuncs
Definition: parsenodes.h:124
Expr * expr
Definition: primnodes.h:1368
static void compare_tlist_datatypes ( List tlist,
List colTypes,
pushdown_safety_info safetyInfo 
)
static

Definition at line 2686 of file allpaths.c.

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

Referenced by subquery_is_pushdown_safe().

2688 {
2689  ListCell *l;
2690  ListCell *colType = list_head(colTypes);
2691 
2692  foreach(l, tlist)
2693  {
2694  TargetEntry *tle = (TargetEntry *) lfirst(l);
2695 
2696  if (tle->resjunk)
2697  continue; /* ignore resjunk columns */
2698  if (colType == NULL)
2699  elog(ERROR, "wrong number of tlist entries");
2700  if (exprType((Node *) tle->expr) != lfirst_oid(colType))
2701  safetyInfo->unsafeColumns[tle->resno] = true;
2702  colType = lnext(colType);
2703  }
2704  if (colType != NULL)
2705  elog(ERROR, "wrong number of tlist entries");
2706 }
Definition: nodes.h:509
bool * unsafeColumns
Definition: allpaths.c:52
bool resjunk
Definition: primnodes.h:1375
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1369
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
#define lnext(lc)
Definition: pg_list.h:105
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
#define elog
Definition: elog.h:219
#define lfirst_oid(lc)
Definition: pg_list.h:108
int compute_parallel_worker ( RelOptInfo rel,
double  heap_pages,
double  index_pages 
)

Definition at line 3067 of file allpaths.c.

References Max, max_parallel_workers_per_gather, 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(), and create_plain_partial_paths().

3068 {
3069  int parallel_workers = 0;
3070 
3071  /*
3072  * If the user has set the parallel_workers reloption, use that; otherwise
3073  * select a default number of workers.
3074  */
3075  if (rel->rel_parallel_workers != -1)
3076  parallel_workers = rel->rel_parallel_workers;
3077  else
3078  {
3079  /*
3080  * If the number of pages being scanned is insufficient to justify a
3081  * parallel scan, just return zero ... unless it's an inheritance
3082  * child. In that case, we want to generate a parallel path here
3083  * anyway. It might not be worthwhile just for this relation, but
3084  * when combined with all of its inheritance siblings it may well pay
3085  * off.
3086  */
3087  if (rel->reloptkind == RELOPT_BASEREL &&
3088  ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3089  (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3090  return 0;
3091 
3092  if (heap_pages >= 0)
3093  {
3094  int heap_parallel_threshold;
3095  int heap_parallel_workers = 1;
3096 
3097  /*
3098  * Select the number of workers based on the log of the size of
3099  * the relation. This probably needs to be a good deal more
3100  * sophisticated, but we need something here for now. Note that
3101  * the upper limit of the min_parallel_table_scan_size GUC is
3102  * chosen to prevent overflow here.
3103  */
3104  heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
3105  while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
3106  {
3107  heap_parallel_workers++;
3108  heap_parallel_threshold *= 3;
3109  if (heap_parallel_threshold > INT_MAX / 3)
3110  break; /* avoid overflow */
3111  }
3112 
3113  parallel_workers = heap_parallel_workers;
3114  }
3115 
3116  if (index_pages >= 0)
3117  {
3118  int index_parallel_workers = 1;
3119  int index_parallel_threshold;
3120 
3121  /* same calculation as for heap_pages above */
3122  index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
3123  while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
3124  {
3125  index_parallel_workers++;
3126  index_parallel_threshold *= 3;
3127  if (index_parallel_threshold > INT_MAX / 3)
3128  break; /* avoid overflow */
3129  }
3130 
3131  if (parallel_workers > 0)
3132  parallel_workers = Min(parallel_workers, index_parallel_workers);
3133  else
3134  parallel_workers = index_parallel_workers;
3135  }
3136  }
3137 
3138  /*
3139  * In no case use more than max_parallel_workers_per_gather workers.
3140  */
3141  parallel_workers = Min(parallel_workers, max_parallel_workers_per_gather);
3142 
3143  return parallel_workers;
3144 }
RelOptKind reloptkind
Definition: relation.h:522
#define Min(x, y)
Definition: c.h:807
uint32 BlockNumber
Definition: block.h:31
int min_parallel_index_scan_size
Definition: allpaths.c:61
int rel_parallel_workers
Definition: relation.h:569
#define Max(x, y)
Definition: c.h:801
int min_parallel_table_scan_size
Definition: allpaths.c:60
int max_parallel_workers_per_gather
Definition: costsize.c:116
void create_partial_bitmap_paths ( PlannerInfo root,
RelOptInfo rel,
Path bitmapqual 
)

Definition at line 3035 of file allpaths.c.

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

Referenced by create_index_paths().

3037 {
3038  int parallel_workers;
3039  double pages_fetched;
3040 
3041  /* Compute heap pages for bitmap heap scan */
3042  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
3043  NULL, NULL);
3044 
3045  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1);
3046 
3047  if (parallel_workers <= 0)
3048  return;
3049 
3050  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3051  bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3052 }
Relids lateral_relids
Definition: relation.h:550
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3067
#define NULL
Definition: c.h:229
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
Definition: relation.h:948
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:5127
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1067
static void create_plain_partial_paths ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 717 of file allpaths.c.

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

Referenced by set_plain_rel_pathlist().

718 {
719  int parallel_workers;
720 
721  parallel_workers = compute_parallel_worker(rel, rel->pages, -1);
722 
723  /* If any limit was set to zero, the user doesn't want a parallel scan. */
724  if (parallel_workers <= 0)
725  return;
726 
727  /* Add an unordered partial path based on a parallel sequential scan. */
728  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
729 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3067
BlockNumber pages
Definition: relation.h:564
#define NULL
Definition: c.h:229
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:938
void generate_gather_paths ( PlannerInfo root,
RelOptInfo rel 
)

Definition at line 2199 of file allpaths.c.

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

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

2200 {
2201  Path *cheapest_partial_path;
2202  Path *simple_gather_path;
2203  ListCell *lc;
2204 
2205  /* If there are no partial paths, there's nothing to do here. */
2206  if (rel->partial_pathlist == NIL)
2207  return;
2208 
2209  /*
2210  * The output of Gather is always unsorted, so there's only one partial
2211  * path of interest: the cheapest one. That will be the one at the front
2212  * of partial_pathlist because of the way add_partial_path works.
2213  */
2214  cheapest_partial_path = linitial(rel->partial_pathlist);
2215  simple_gather_path = (Path *)
2216  create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2217  NULL, NULL);
2218  add_path(rel, simple_gather_path);
2219 
2220  /*
2221  * For each useful ordering, we can consider an order-preserving Gather
2222  * Merge.
2223  */
2224  foreach(lc, rel->partial_pathlist)
2225  {
2226  Path *subpath = (Path *) lfirst(lc);
2227  GatherMergePath *path;
2228 
2229  if (subpath->pathkeys == NIL)
2230  continue;
2231 
2232  path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2233  subpath->pathkeys, NULL, NULL);
2234  add_path(rel, &path->path);
2235  }
2236 }
#define NIL
Definition: pg_list.h:69
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
Definition: pathnode.c:1730
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
List * partial_pathlist
Definition: relation.h:541
#define linitial(l)
Definition: pg_list.h:111
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1639
List * pathkeys
Definition: relation.h:968
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
struct PathTarget * reltarget
Definition: relation.h:536
Definition: relation.h:948
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
static void generate_mergeappend_paths ( PlannerInfo root,
RelOptInfo rel,
List live_childrels,
List all_child_pathkeys,
List partitioned_rels 
)
static

Definition at line 1508 of file allpaths.c.

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

Referenced by add_paths_to_append_rel().

1512 {
1513  ListCell *lcp;
1514 
1515  foreach(lcp, all_child_pathkeys)
1516  {
1517  List *pathkeys = (List *) lfirst(lcp);
1518  List *startup_subpaths = NIL;
1519  List *total_subpaths = NIL;
1520  bool startup_neq_total = false;
1521  ListCell *lcr;
1522 
1523  /* Select the child paths for this ordering... */
1524  foreach(lcr, live_childrels)
1525  {
1526  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1527  Path *cheapest_startup,
1528  *cheapest_total;
1529 
1530  /* Locate the right paths, if they are available. */
1531  cheapest_startup =
1533  pathkeys,
1534  NULL,
1535  STARTUP_COST,
1536  false);
1537  cheapest_total =
1539  pathkeys,
1540  NULL,
1541  TOTAL_COST,
1542  false);
1543 
1544  /*
1545  * If we can't find any paths with the right order just use the
1546  * cheapest-total path; we'll have to sort it later.
1547  */
1548  if (cheapest_startup == NULL || cheapest_total == NULL)
1549  {
1550  cheapest_startup = cheapest_total =
1551  childrel->cheapest_total_path;
1552  /* Assert we do have an unparameterized path for this child */
1553  Assert(cheapest_total->param_info == NULL);
1554  }
1555 
1556  /*
1557  * Notice whether we actually have different paths for the
1558  * "cheapest" and "total" cases; frequently there will be no point
1559  * in two create_merge_append_path() calls.
1560  */
1561  if (cheapest_startup != cheapest_total)
1562  startup_neq_total = true;
1563 
1564  startup_subpaths =
1565  accumulate_append_subpath(startup_subpaths, cheapest_startup);
1566  total_subpaths =
1567  accumulate_append_subpath(total_subpaths, cheapest_total);
1568  }
1569 
1570  /* ... and build the MergeAppend paths */
1571  add_path(rel, (Path *) create_merge_append_path(root,
1572  rel,
1573  startup_subpaths,
1574  pathkeys,
1575  NULL,
1576  partitioned_rels));
1577  if (startup_neq_total)
1578  add_path(rel, (Path *) create_merge_append_path(root,
1579  rel,
1580  total_subpaths,
1581  pathkeys,
1582  NULL,
1583  partitioned_rels));
1584  }
1585 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:343
#define NIL
Definition: pg_list.h:69
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
ParamPathInfo * param_info
Definition: relation.h:957
struct Path * cheapest_total_path
Definition: relation.h:543
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer, List *partitioned_rels)
Definition: pathnode.c:1257
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
static List * accumulate_append_subpath(List *subpaths, Path *path)
Definition: allpaths.c:1676
List * pathlist
Definition: relation.h:539
Definition: pg_list.h:45
Definition: relation.h:948
static Path * get_cheapest_parameterized_child_path ( PlannerInfo root,
RelOptInfo rel,
Relids  required_outer 
)
static

Definition at line 1595 of file allpaths.c.

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

Referenced by add_paths_to_append_rel().

1597 {
1598  Path *cheapest;
1599  ListCell *lc;
1600 
1601  /*
1602  * Look up the cheapest existing path with no more than the needed
1603  * parameterization. If it has exactly the needed parameterization, we're
1604  * done.
1605  */
1606  cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
1607  NIL,
1608  required_outer,
1609  TOTAL_COST,
1610  false);
1611  Assert(cheapest != NULL);
1612  if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
1613  return cheapest;
1614 
1615  /*
1616  * Otherwise, we can "reparameterize" an existing path to match the given
1617  * parameterization, which effectively means pushing down additional
1618  * joinquals to be checked within the path's scan. However, some existing
1619  * paths might check the available joinquals already while others don't;
1620  * therefore, it's not clear which existing path will be cheapest after
1621  * reparameterization. We have to go through them all and find out.
1622  */
1623  cheapest = NULL;
1624  foreach(lc, rel->pathlist)
1625  {
1626  Path *path = (Path *) lfirst(lc);
1627 
1628  /* Can't use it if it needs more than requested parameterization */
1629  if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
1630  continue;
1631 
1632  /*
1633  * Reparameterization can only increase the path's cost, so if it's
1634  * already more expensive than the current cheapest, forget it.
1635  */
1636  if (cheapest != NULL &&
1637  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1638  continue;
1639 
1640  /* Reparameterize if needed, then recheck cost */
1641  if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
1642  {
1643  path = reparameterize_path(root, path, required_outer, 1.0);
1644  if (path == NULL)
1645  continue; /* failed to reparameterize this one */
1646  Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
1647 
1648  if (cheapest != NULL &&
1649  compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1650  continue;
1651  }
1652 
1653  /* We have a new best path */
1654  cheapest = path;
1655  }
1656 
1657  /* Return the best path, or NULL if we found no suitable candidate */
1658  return cheapest;
1659 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:343
#define NIL
Definition: pg_list.h:69
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:61
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
#define PATH_REQ_OUTER(path)
Definition: relation.h:973
Path * reparameterize_path(PlannerInfo *root, Path *path, Relids required_outer, double loop_count)
Definition: pathnode.c:3373
List * pathlist
Definition: relation.h:539
Definition: relation.h:948
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
static bool has_multiple_baserels ( PlannerInfo root)
static

Definition at line 1729 of file allpaths.c.

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

Referenced by set_subquery_pathlist().

1730 {
1731  int num_base_rels = 0;
1732  Index rti;
1733 
1734  for (rti = 1; rti < root->simple_rel_array_size; rti++)
1735  {
1736  RelOptInfo *brel = root->simple_rel_array[rti];
1737 
1738  if (brel == NULL)
1739  continue;
1740 
1741  /* ignore RTEs that are "other rels" */
1742  if (brel->reloptkind == RELOPT_BASEREL)
1743  if (++num_base_rels > 1)
1744  return true;
1745  }
1746  return false;
1747 }
RelOptKind reloptkind
Definition: relation.h:522
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
int simple_rel_array_size
Definition: relation.h:180
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
RelOptInfo* make_one_rel ( PlannerInfo root,
List joinlist 
)

Definition at line 145 of file allpaths.c.

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

Referenced by query_planner().

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

Definition at line 2246 of file allpaths.c.

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

Referenced by make_one_rel().

2247 {
2248  int levels_needed;
2249  List *initial_rels;
2250  ListCell *jl;
2251 
2252  /*
2253  * Count the number of child joinlist nodes. This is the depth of the
2254  * dynamic-programming algorithm we must employ to consider all ways of
2255  * joining the child nodes.
2256  */
2257  levels_needed = list_length(joinlist);
2258 
2259  if (levels_needed <= 0)
2260  return NULL; /* nothing to do? */
2261 
2262  /*
2263  * Construct a list of rels corresponding to the child joinlist nodes.
2264  * This may contain both base rels and rels constructed according to
2265  * sub-joinlists.
2266  */
2267  initial_rels = NIL;
2268  foreach(jl, joinlist)
2269  {
2270  Node *jlnode = (Node *) lfirst(jl);
2271  RelOptInfo *thisrel;
2272 
2273  if (IsA(jlnode, RangeTblRef))
2274  {
2275  int varno = ((RangeTblRef *) jlnode)->rtindex;
2276 
2277  thisrel = find_base_rel(root, varno);
2278  }
2279  else if (IsA(jlnode, List))
2280  {
2281  /* Recurse to handle subproblem */
2282  thisrel = make_rel_from_joinlist(root, (List *) jlnode);
2283  }
2284  else
2285  {
2286  elog(ERROR, "unrecognized joinlist node type: %d",
2287  (int) nodeTag(jlnode));
2288  thisrel = NULL; /* keep compiler quiet */
2289  }
2290 
2291  initial_rels = lappend(initial_rels, thisrel);
2292  }
2293 
2294  if (levels_needed == 1)
2295  {
2296  /*
2297  * Single joinlist node, so we're done.
2298  */
2299  return (RelOptInfo *) linitial(initial_rels);
2300  }
2301  else
2302  {
2303  /*
2304  * Consider the different orders in which we could join the rels,
2305  * using a plugin, GEQO, or the regular join search code.
2306  *
2307  * We put the initial_rels list into a PlannerInfo field because
2308  * has_legal_joinclause() needs to look at it (ugly :-().
2309  */
2310  root->initial_rels = initial_rels;
2311 
2312  if (join_search_hook)
2313  return (*join_search_hook) (root, levels_needed, initial_rels);
2314  else if (enable_geqo && levels_needed >= geqo_threshold)
2315  return geqo(root, levels_needed, initial_rels);
2316  else
2317  return standard_join_search(root, levels_needed, initial_rels);
2318  }
2319 }
RelOptInfo * standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
Definition: allpaths.c:2351
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: nodes.h:509
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
bool enable_geqo
Definition: allpaths.c:58
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:2246
List * lappend(List *list, void *datum)
Definition: list.c:128
join_search_hook_type join_search_hook
Definition: allpaths.c:67
int geqo_threshold
Definition: allpaths.c:59
RelOptInfo * geqo(PlannerInfo *root, int number_of_rels, List *initial_rels)
Definition: geqo_main.c:67
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
#define nodeTag(nodeptr)
Definition: nodes.h:514
List * initial_rels
Definition: relation.h:269
#define elog
Definition: elog.h:219
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
Definition: pg_list.h:45
static bool qual_is_pushdown_safe ( Query subquery,
Index  rti,
Node qual,
pushdown_safety_info safetyInfo 
)
static

Definition at line 2759 of file allpaths.c.

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

Referenced by set_subquery_pathlist().

2761 {
2762  bool safe = true;
2763  List *vars;
2764  ListCell *vl;
2765 
2766  /* Refuse subselects (point 1) */
2767  if (contain_subplans(qual))
2768  return false;
2769 
2770  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
2771  if (safetyInfo->unsafeVolatile &&
2773  return false;
2774 
2775  /* Refuse leaky quals if told to (point 3) */
2776  if (safetyInfo->unsafeLeaky &&
2777  contain_leaked_vars(qual))
2778  return false;
2779 
2780  /*
2781  * It would be unsafe to push down window function calls, but at least for
2782  * the moment we could never see any in a qual anyhow. (The same applies
2783  * to aggregates, which we check for in pull_var_clause below.)
2784  */
2786 
2787  /*
2788  * Examine all Vars used in clause; since it's a restriction clause, all
2789  * such Vars must refer to subselect output columns.
2790  */
2792  foreach(vl, vars)
2793  {
2794  Var *var = (Var *) lfirst(vl);
2795 
2796  /*
2797  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
2798  * It's not clear whether a PHV could safely be pushed down, and even
2799  * less clear whether such a situation could arise in any cases of
2800  * practical interest anyway. So for the moment, just refuse to push
2801  * down.
2802  */
2803  if (!IsA(var, Var))
2804  {
2805  safe = false;
2806  break;
2807  }
2808 
2809  Assert(var->varno == rti);
2810  Assert(var->varattno >= 0);
2811 
2812  /* Check point 4 */
2813  if (var->varattno == 0)
2814  {
2815  safe = false;
2816  break;
2817  }
2818 
2819  /* Check point 5 */
2820  if (safetyInfo->unsafeColumns[var->varattno])
2821  {
2822  safe = false;
2823  break;
2824  }
2825  }
2826 
2827  list_free(vars);
2828 
2829  return safe;
2830 }
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1481
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
AttrNumber varattno
Definition: primnodes.h:168
bool * unsafeColumns
Definition: allpaths.c:52
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:957
Definition: primnodes.h:163
bool contain_subplans(Node *clause)
Definition: clauses.c:843
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
bool contain_window_function(Node *clause)
Definition: clauses.c:727
Index varno
Definition: primnodes.h:166
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
void list_free(List *list)
Definition: list.c:1133
Definition: regcomp.c:224
Definition: pg_list.h:45
static void recurse_push_qual ( Node setOp,
Query topquery,
RangeTblEntry rte,
Index  rti,
Node qual 
)
static

Definition at line 2883 of file allpaths.c.

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

Referenced by subquery_push_qual().

2885 {
2886  if (IsA(setOp, RangeTblRef))
2887  {
2888  RangeTblRef *rtr = (RangeTblRef *) setOp;
2889  RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
2890  Query *subquery = subrte->subquery;
2891 
2892  Assert(subquery != NULL);
2893  subquery_push_qual(subquery, rte, rti, qual);
2894  }
2895  else if (IsA(setOp, SetOperationStmt))
2896  {
2897  SetOperationStmt *op = (SetOperationStmt *) setOp;
2898 
2899  recurse_push_qual(op->larg, topquery, rte, rti, qual);
2900  recurse_push_qual(op->rarg, topquery, rte, rti, qual);
2901  }
2902  else
2903  {
2904  elog(ERROR, "unrecognized node type: %d",
2905  (int) nodeTag(setOp));
2906  }
2907 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * rtable
Definition: parsenodes.h:135
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2836
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2883
#define nodeTag(nodeptr)
Definition: nodes.h:514
Query * subquery
Definition: parsenodes.h:967
#define elog
Definition: elog.h:219
static bool recurse_pushdown_safe ( Node setOp,
Query topquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 2551 of file allpaths.c.

References Assert, elog, ERROR, IsA, SetOperationStmt::larg, nodeTag, NULL, 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().

2553 {
2554  if (IsA(setOp, RangeTblRef))
2555  {
2556  RangeTblRef *rtr = (RangeTblRef *) setOp;
2557  RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
2558  Query *subquery = rte->subquery;
2559 
2560  Assert(subquery != NULL);
2561  return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
2562  }
2563  else if (IsA(setOp, SetOperationStmt))
2564  {
2565  SetOperationStmt *op = (SetOperationStmt *) setOp;
2566 
2567  /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
2568  if (op->op == SETOP_EXCEPT)
2569  return false;
2570  /* Else recurse */
2571  if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
2572  return false;
2573  if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
2574  return false;
2575  }
2576  else
2577  {
2578  elog(ERROR, "unrecognized node type: %d",
2579  (int) nodeTag(setOp));
2580  }
2581  return true;
2582 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2551
List * rtable
Definition: parsenodes.h:135
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
SetOperation op
Definition: parsenodes.h:1568
#define nodeTag(nodeptr)
Definition: nodes.h:514
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2499
Query * subquery
Definition: parsenodes.h:967
#define elog
Definition: elog.h:219
static void remove_unused_subquery_outputs ( Query subquery,
RelOptInfo rel 
)
static

Definition at line 2930 of file allpaths.c.

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

Referenced by set_subquery_pathlist().

2931 {
2932  Bitmapset *attrs_used = NULL;
2933  ListCell *lc;
2934 
2935  /*
2936  * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
2937  * could update all the child SELECTs' tlists, but it seems not worth the
2938  * trouble presently.
2939  */
2940  if (subquery->setOperations)
2941  return;
2942 
2943  /*
2944  * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
2945  * time: all its output columns must be used in the distinctClause.
2946  */
2947  if (subquery->distinctClause && !subquery->hasDistinctOn)
2948  return;
2949 
2950  /*
2951  * Collect a bitmap of all the output column numbers used by the upper
2952  * query.
2953  *
2954  * Add all the attributes needed for joins or final output. Note: we must
2955  * look at rel's targetlist, not the attr_needed data, because attr_needed
2956  * isn't computed for inheritance child rels, cf set_append_rel_size().
2957  * (XXX might be worth changing that sometime.)
2958  */
2959  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
2960 
2961  /* Add all the attributes used by un-pushed-down restriction clauses. */
2962  foreach(lc, rel->baserestrictinfo)
2963  {
2964  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2965 
2966  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
2967  }
2968 
2969  /*
2970  * If there's a whole-row reference to the subquery, we can't remove
2971  * anything.
2972  */
2974  return;
2975 
2976  /*
2977  * Run through the tlist and zap entries we don't need. It's okay to
2978  * modify the tlist items in-place because set_subquery_pathlist made a
2979  * copy of the subquery.
2980  */
2981  foreach(lc, subquery->targetList)
2982  {
2983  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2984  Node *texpr = (Node *) tle->expr;
2985 
2986  /*
2987  * If it has a sortgroupref number, it's used in some sort/group
2988  * clause so we'd better not remove it. Also, don't remove any
2989  * resjunk columns, since their reason for being has nothing to do
2990  * with anybody reading the subquery's output. (It's likely that
2991  * resjunk columns in a sub-SELECT would always have ressortgroupref
2992  * set, but even if they don't, it seems imprudent to remove them.)
2993  */
2994  if (tle->ressortgroupref || tle->resjunk)
2995  continue;
2996 
2997  /*
2998  * If it's used by the upper query, we can't remove it.
2999  */
3001  attrs_used))
3002  continue;
3003 
3004  /*
3005  * If it contains a set-returning function, we can't remove it since
3006  * that could change the number of rows returned by the subquery.
3007  */
3008  if (subquery->hasTargetSRFs &&
3009  expression_returns_set(texpr))
3010  continue;
3011 
3012  /*
3013  * If it contains volatile functions, we daren't remove it for fear
3014  * that the user is expecting their side-effects to happen.
3015  */
3016  if (contain_volatile_functions(texpr))
3017  continue;
3018 
3019  /*
3020  * OK, we don't need it. Replace the expression with a NULL constant.
3021  * Preserve the exposed type of the expression, in case something
3022  * looks at the rowtype of the subquery's result.
3023  */
3024  tle->expr = (Expr *) makeNullConst(exprType(texpr),
3025  exprTypmod(texpr),
3026  exprCollation(texpr));
3027  }
3028 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
List * baserestrictinfo
Definition: relation.h:585
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:509
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:957
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool hasDistinctOn
Definition: parsenodes.h:127
List * targetList
Definition: parsenodes.h:138
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
bool resjunk
Definition: primnodes.h:1375
List * distinctClause
Definition: parsenodes.h:154
AttrNumber resno
Definition: primnodes.h:1369
Index relid
Definition: relation.h:553
Expr * clause
Definition: relation.h:1747
List * exprs
Definition: relation.h:884
bool hasTargetSRFs
Definition: parsenodes.h:125
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
Node * setOperations
Definition: parsenodes.h:163
Index ressortgroupref
Definition: primnodes.h:1371
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
struct PathTarget * reltarget
Definition: relation.h:536
static void set_append_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 1208 of file allpaths.c.

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

Referenced by set_rel_pathlist().

1210 {
1211  int parentRTindex = rti;
1212  List *live_childrels = NIL;
1213  ListCell *l;
1214 
1215  /*
1216  * Generate access paths for each member relation, and remember the
1217  * non-dummy children.
1218  */
1219  foreach(l, root->append_rel_list)
1220  {
1221  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1222  int childRTindex;
1223  RangeTblEntry *childRTE;
1224  RelOptInfo *childrel;
1225 
1226  /* append_rel_list contains all append rels; ignore others */
1227  if (appinfo->parent_relid != parentRTindex)
1228  continue;
1229 
1230  /* Re-locate the child RTE and RelOptInfo */
1231  childRTindex = appinfo->child_relid;
1232  childRTE = root->simple_rte_array[childRTindex];
1233  childrel = root->simple_rel_array[childRTindex];
1234 
1235  /*
1236  * If set_append_rel_size() decided the parent appendrel was
1237  * parallel-unsafe at some point after visiting this child rel, we
1238  * need to propagate the unsafety marking down to the child, so that
1239  * we don't generate useless partial paths for it.
1240  */
1241  if (!rel->consider_parallel)
1242  childrel->consider_parallel = false;
1243 
1244  /*
1245  * Compute the child's access paths.
1246  */
1247  set_rel_pathlist(root, childrel, childRTindex, childRTE);
1248 
1249  /*
1250  * If child is dummy, ignore it.
1251  */
1252  if (IS_DUMMY_REL(childrel))
1253  continue;
1254 
1255  /*
1256  * Child is live, so add it to the live_childrels list for use below.
1257  */
1258  live_childrels = lappend(live_childrels, childrel);
1259  }
1260 
1261  /* Add paths to the "append" relation. */
1262  add_paths_to_append_rel(root, rel, live_childrels);
1263 }
#define NIL
Definition: pg_list.h:69
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
List * lappend(List *list, void *datum)
Definition: list.c:128
RangeTblEntry ** simple_rte_array
Definition: relation.h:188
List * append_rel_list
Definition: relation.h:252
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:421
#define lfirst(lc)
Definition: pg_list.h:106
bool consider_parallel
Definition: relation.h:533
static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1278
Index child_relid
Definition: relation.h:1978
Index parent_relid
Definition: relation.h:1977
Definition: pg_list.h:45
static void set_append_rel_size ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 859 of file allpaths.c.

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

Referenced by set_rel_size().

861 {
862  int parentRTindex = rti;
863  bool has_live_children;
864  double parent_rows;
865  double parent_size;
866  double *parent_attrsizes;
867  int nattrs;
868  ListCell *l;
869 
870  Assert(IS_SIMPLE_REL(rel));
871 
872  /*
873  * Initialize to compute size estimates for whole append relation.
874  *
875  * We handle width estimates by weighting the widths of different child
876  * rels proportionally to their number of rows. This is sensible because
877  * the use of width estimates is mainly to compute the total relation
878  * "footprint" if we have to sort or hash it. To do this, we sum the
879  * total equivalent size (in "double" arithmetic) and then divide by the
880  * total rowcount estimate. This is done separately for the total rel
881  * width and each attribute.
882  *
883  * Note: if you consider changing this logic, beware that child rels could
884  * have zero rows and/or width, if they were excluded by constraints.
885  */
886  has_live_children = false;
887  parent_rows = 0;
888  parent_size = 0;
889  nattrs = rel->max_attr - rel->min_attr + 1;
890  parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
891 
892  foreach(l, root->append_rel_list)
893  {
894  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
895  int childRTindex;
896  RangeTblEntry *childRTE;
897  RelOptInfo *childrel;
898  List *childquals;
899  Index cq_min_security;
900  bool have_const_false_cq;
901  ListCell *parentvars;
902  ListCell *childvars;
903  ListCell *lc;
904 
905  /* append_rel_list contains all append rels; ignore others */
906  if (appinfo->parent_relid != parentRTindex)
907  continue;
908 
909  childRTindex = appinfo->child_relid;
910  childRTE = root->simple_rte_array[childRTindex];
911 
912  /*
913  * The child rel's RelOptInfo was already created during
914  * add_base_rels_to_query.
915  */
916  childrel = find_base_rel(root, childRTindex);
918 
919  /*
920  * We have to copy the parent's targetlist and quals to the child,
921  * with appropriate substitution of variables. However, only the
922  * baserestrictinfo quals are needed before we can check for
923  * constraint exclusion; so do that first and then check to see if we
924  * can disregard this child.
925  *
926  * The child rel's targetlist might contain non-Var expressions, which
927  * means that substitution into the quals could produce opportunities
928  * for const-simplification, and perhaps even pseudoconstant quals.
929  * Therefore, transform each RestrictInfo separately to see if it
930  * reduces to a constant or pseudoconstant. (We must process them
931  * separately to keep track of the security level of each qual.)
932  */
933  childquals = NIL;
934  cq_min_security = UINT_MAX;
935  have_const_false_cq = false;
936  foreach(lc, rel->baserestrictinfo)
937  {
938  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
939  Node *childqual;
940  ListCell *lc2;
941 
942  Assert(IsA(rinfo, RestrictInfo));
943  childqual = adjust_appendrel_attrs(root,
944  (Node *) rinfo->clause,
945  1, &appinfo);
946  childqual = eval_const_expressions(root, childqual);
947  /* check for flat-out constant */
948  if (childqual && IsA(childqual, Const))
949  {
950  if (((Const *) childqual)->constisnull ||
951  !DatumGetBool(((Const *) childqual)->constvalue))
952  {
953  /* Restriction reduces to constant FALSE or NULL */
954  have_const_false_cq = true;
955  break;
956  }
957  /* Restriction reduces to constant TRUE, so drop it */
958  continue;
959  }
960  /* might have gotten an AND clause, if so flatten it */
961  foreach(lc2, make_ands_implicit((Expr *) childqual))
962  {
963  Node *onecq = (Node *) lfirst(lc2);
964  bool pseudoconstant;
965 
966  /* check for pseudoconstant (no Vars or volatile functions) */
967  pseudoconstant =
968  !contain_vars_of_level(onecq, 0) &&
970  if (pseudoconstant)
971  {
972  /* tell createplan.c to check for gating quals */
973  root->hasPseudoConstantQuals = true;
974  }
975  /* reconstitute RestrictInfo with appropriate properties */
976  childquals = lappend(childquals,
977  make_restrictinfo((Expr *) onecq,
978  rinfo->is_pushed_down,
979  rinfo->outerjoin_delayed,
980  pseudoconstant,
981  rinfo->security_level,
982  NULL, NULL, NULL));
983  /* track minimum security level among child quals */
984  cq_min_security = Min(cq_min_security, rinfo->security_level);
985  }
986  }
987 
988  /*
989  * In addition to the quals inherited from the parent, we might have
990  * securityQuals associated with this particular child node.
991  * (Currently this can only happen in appendrels originating from
992  * UNION ALL; inheritance child tables don't have their own
993  * securityQuals, see expand_inherited_rtentry().) Pull any such
994  * securityQuals up into the baserestrictinfo for the child. This is
995  * similar to process_security_barrier_quals() for the parent rel,
996  * except that we can't make any general deductions from such quals,
997  * since they don't hold for the whole appendrel.
998  */
999  if (childRTE->securityQuals)
1000  {
1001  Index security_level = 0;
1002 
1003  foreach(lc, childRTE->securityQuals)
1004  {
1005  List *qualset = (List *) lfirst(lc);
1006  ListCell *lc2;
1007 
1008  foreach(lc2, qualset)
1009  {
1010  Expr *qual = (Expr *) lfirst(lc2);
1011 
1012  /* not likely that we'd see constants here, so no check */
1013  childquals = lappend(childquals,
1014  make_restrictinfo(qual,
1015  true, false, false,
1016  security_level,
1017  NULL, NULL, NULL));
1018  cq_min_security = Min(cq_min_security, security_level);
1019  }
1020  security_level++;
1021  }
1022  Assert(security_level <= root->qual_security_level);
1023  }
1024 
1025  /*
1026  * OK, we've got all the baserestrictinfo quals for this child.
1027  */
1028  childrel->baserestrictinfo = childquals;
1029  childrel->baserestrict_min_security = cq_min_security;
1030 
1031  if (have_const_false_cq)
1032  {
1033  /*
1034  * Some restriction clause reduced to constant FALSE or NULL after
1035  * substitution, so this child need not be scanned.
1036  */
1037  set_dummy_rel_pathlist(childrel);
1038  continue;
1039  }
1040 
1041  if (relation_excluded_by_constraints(root, childrel, childRTE))
1042  {
1043  /*
1044  * This child need not be scanned, so we can omit it from the
1045  * appendrel.
1046  */
1047  set_dummy_rel_pathlist(childrel);
1048  continue;
1049  }
1050 
1051  /*
1052  * CE failed, so finish copying/modifying targetlist and join quals.
1053  *
1054  * NB: the resulting childrel->reltarget->exprs may contain arbitrary
1055  * expressions, which otherwise would not occur in a rel's targetlist.
1056  * Code that might be looking at an appendrel child must cope with
1057  * such. (Normally, a rel's targetlist would only include Vars and
1058  * PlaceHolderVars.) XXX we do not bother to update the cost or width
1059  * fields of childrel->reltarget; not clear if that would be useful.
1060  */
1061  childrel->joininfo = (List *)
1063  (Node *) rel->joininfo,
1064  1, &appinfo);
1065  childrel->reltarget->exprs = (List *)
1067  (Node *) rel->reltarget->exprs,
1068  1, &appinfo);
1069 
1070  /*
1071  * We have to make child entries in the EquivalenceClass data
1072  * structures as well. This is needed either if the parent
1073  * participates in some eclass joins (because we will want to consider
1074  * inner-indexscan joins on the individual children) or if the parent
1075  * has useful pathkeys (because we should try to build MergeAppend
1076  * paths that produce those sort orderings).
1077  */
1078  if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1079  add_child_rel_equivalences(root, appinfo, rel, childrel);
1080  childrel->has_eclass_joins = rel->has_eclass_joins;
1081 
1082  /*
1083  * Note: we could compute appropriate attr_needed data for the child's
1084  * variables, by transforming the parent's attr_needed through the
1085  * translated_vars mapping. However, currently there's no need
1086  * because attr_needed is only examined for base relations not
1087  * otherrels. So we just leave the child's attr_needed empty.
1088  */
1089 
1090  /*
1091  * If parallelism is allowable for this query in general, see whether
1092  * it's allowable for this childrel in particular. But if we've
1093  * already decided the appendrel is not parallel-safe as a whole,
1094  * there's no point in considering parallelism for this child. For
1095  * consistency, do this before calling set_rel_size() for the child.
1096  */
1097  if (root->glob->parallelModeOK && rel->consider_parallel)
1098  set_rel_consider_parallel(root, childrel, childRTE);
1099 
1100  /*
1101  * Compute the child's size.
1102  */
1103  set_rel_size(root, childrel, childRTindex, childRTE);
1104 
1105  /*
1106  * It is possible that constraint exclusion detected a contradiction
1107  * within a child subquery, even though we didn't prove one above. If
1108  * so, we can skip this child.
1109  */
1110  if (IS_DUMMY_REL(childrel))
1111  continue;
1112 
1113  /* We have at least one live child. */
1114  has_live_children = true;
1115 
1116  /*
1117  * If any live child is not parallel-safe, treat the whole appendrel
1118  * as not parallel-safe. In future we might be able to generate plans
1119  * in which some children are farmed out to workers while others are
1120  * not; but we don't have that today, so it's a waste to consider
1121  * partial paths anywhere in the appendrel unless it's all safe.
1122  * (Child rels visited before this one will be unmarked in
1123  * set_append_rel_pathlist().)
1124  */
1125  if (!childrel->consider_parallel)
1126  rel->consider_parallel = false;
1127 
1128  /*
1129  * Accumulate size information from each live child.
1130  */
1131  Assert(childrel->rows > 0);
1132 
1133  parent_rows += childrel->rows;
1134  parent_size += childrel->reltarget->width * childrel->rows;
1135 
1136  /*
1137  * Accumulate per-column estimates too. We need not do anything for
1138  * PlaceHolderVars in the parent list. If child expression isn't a
1139  * Var, or we didn't record a width estimate for it, we have to fall
1140  * back on a datatype-based estimate.
1141  *
1142  * By construction, child's targetlist is 1-to-1 with parent's.
1143  */
1144  forboth(parentvars, rel->reltarget->exprs,
1145  childvars, childrel->reltarget->exprs)
1146  {
1147  Var *parentvar = (Var *) lfirst(parentvars);
1148  Node *childvar = (Node *) lfirst(childvars);
1149 
1150  if (IsA(parentvar, Var))
1151  {
1152  int pndx = parentvar->varattno - rel->min_attr;
1153  int32 child_width = 0;
1154 
1155  if (IsA(childvar, Var) &&
1156  ((Var *) childvar)->varno == childrel->relid)
1157  {
1158  int cndx = ((Var *) childvar)->varattno - childrel->min_attr;
1159 
1160  child_width = childrel->attr_widths[cndx];
1161  }
1162  if (child_width <= 0)
1163  child_width = get_typavgwidth(exprType(childvar),
1164  exprTypmod(childvar));
1165  Assert(child_width > 0);
1166  parent_attrsizes[pndx] += child_width * childrel->rows;
1167  }
1168  }
1169  }
1170 
1171  if (has_live_children)
1172  {
1173  /*
1174  * Save the finished size estimates.
1175  */
1176  int i;
1177 
1178  Assert(parent_rows > 0);
1179  rel->rows = parent_rows;
1180  rel->reltarget->width = rint(parent_size / parent_rows);
1181  for (i = 0; i < nattrs; i++)
1182  rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1183 
1184  /*
1185  * Set "raw tuples" count equal to "rows" for the appendrel; needed
1186  * because some places assume rel->tuples is valid for any baserel.
1187  */
1188  rel->tuples = parent_rows;
1189  }
1190  else
1191  {
1192  /*
1193  * All children were excluded by constraints, so mark the whole
1194  * appendrel dummy. We must do this in this phase so that the rel's
1195  * dummy-ness is visible when we generate paths for other rels.
1196  */
1198  }
1199 
1200  pfree(parent_attrsizes);
1201 }
bool has_eclass_joins
Definition: relation.h:591
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index security_level
Definition: relation.h:1759
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
RelOptKind reloptkind
Definition: relation.h:522
RestrictInfo * make_restrictinfo(Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:57
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
List * securityQuals
Definition: parsenodes.h:1052
double tuples
Definition: relation.h:565
List * baserestrictinfo
Definition: relation.h:585
#define Min(x, y)
Definition: c.h:807
bool relation_excluded_by_constraints(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: plancat.c:1345
Definition: nodes.h:509
AttrNumber varattno
Definition: primnodes.h:168
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2421
Index baserestrict_min_security
Definition: relation.h:587
void add_child_rel_equivalences(PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel)
Definition: equivclass.c:2070
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:957
Definition: primnodes.h:163
#define IS_SIMPLE_REL(rel)
Definition: relation.h:505
signed int int32
Definition: c.h:256
void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:1706
void pfree(void *pointer)
Definition: mcxt.c:950
List * make_ands_implicit(Expr *clause)
Definition: clauses.c:378
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
bool parallelModeOK
Definition: relation.h:129
bool outerjoin_delayed
Definition: relation.h:1751
static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:529
List * joininfo
Definition: relation.h:589
PlannerGlobal * glob
Definition: relation.h:157
#define DatumGetBool(X)
Definition: postgres.h:399
double rint(double x)
Definition: rint.c:22
Index relid
Definition: relation.h:553
List * lappend(List *list, void *datum)
Definition: list.c:128
RangeTblEntry ** simple_rte_array
Definition: relation.h:188
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: prepunion.c:1792
Expr * clause
Definition: relation.h:1747
List * exprs
Definition: relation.h:884
void * palloc0(Size size)
Definition: mcxt.c:878
List * append_rel_list
Definition: relation.h:252
unsigned int Index
Definition: c.h:365
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2328
double rows
Definition: relation.h:528
bool hasPseudoConstantQuals
Definition: relation.h:303
bool is_pushed_down
Definition: relation.h:1749
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool consider_parallel
Definition: relation.h:533
int width
Definition: relation.h:887
AttrNumber max_attr
Definition: relation.h:557
int i
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1558
Index child_relid
Definition: relation.h:1978
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
Index parent_relid
Definition: relation.h:1977
int32 * attr_widths
Definition: relation.h:559
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:536
static void set_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:318
AttrNumber min_attr
Definition: relation.h:556
static void set_base_rel_consider_startup ( PlannerInfo root)
static

Definition at line 205 of file allpaths.c.

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

Referenced by make_one_rel().

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

Definition at line 291 of file allpaths.c.

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

Referenced by make_one_rel().

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

Definition at line 248 of file allpaths.c.

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

Referenced by make_one_rel().

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

Definition at line 2051 of file allpaths.c.

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

Referenced by set_rel_size().

2052 {
2053  Plan *cteplan;
2054  PlannerInfo *cteroot;
2055  Index levelsup;
2056  int ndx;
2057  ListCell *lc;
2058  int plan_id;
2059  Relids required_outer;
2060 
2061  /*
2062  * Find the referenced CTE, and locate the plan previously made for it.
2063  */
2064  levelsup = rte->ctelevelsup;
2065  cteroot = root;
2066  while (levelsup-- > 0)
2067  {
2068  cteroot = cteroot->parent_root;
2069  if (!cteroot) /* shouldn't happen */
2070  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2071  }
2072 
2073  /*
2074  * Note: cte_plan_ids can be shorter than cteList, if we are still working
2075  * on planning the CTEs (ie, this is a side-reference from another CTE).
2076  * So we mustn't use forboth here.
2077  */
2078  ndx = 0;
2079  foreach(lc, cteroot->parse->cteList)
2080  {
2081  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2082 
2083  if (strcmp(cte->ctename, rte->ctename) == 0)
2084  break;
2085  ndx++;
2086  }
2087  if (lc == NULL) /* shouldn't happen */
2088  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2089  if (ndx >= list_length(cteroot->cte_plan_ids))
2090  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2091  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2092  Assert(plan_id > 0);
2093  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2094 
2095  /* Mark rel with estimated output rows, width, etc */
2096  set_cte_size_estimates(root, rel, cteplan->plan_rows);
2097 
2098  /*
2099  * We don't support pushing join clauses into the quals of a CTE scan, but
2100  * it could still have required parameterization due to LATERAL refs in
2101  * its tlist.
2102  */
2103  required_outer = rel->lateral_relids;
2104 
2105  /* Generate appropriate path */
2106  add_path(rel, create_ctescan_path(root, rel, required_outer));
2107 }
double plan_rows
Definition: plannodes.h:131
Query * parse
Definition: relation.h:155
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:4730
Relids lateral_relids
Definition: relation.h:550
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
List * subplans
Definition: relation.h:98
PlannerGlobal * glob
Definition: relation.h:157
struct PlannerInfo * parent_root
Definition: relation.h:161
int list_nth_int(const List *list, int n)
Definition: list.c:421
List * cte_plan_ids
Definition: relation.h:230
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Index ctelevelsup
Definition: parsenodes.h:1015
List * cteList
Definition: parsenodes.h:133
char * ctename
Definition: parsenodes.h:1014
#define elog
Definition: elog.h:219
Path * create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1875
void set_dummy_rel_pathlist ( RelOptInfo rel)

Definition at line 1706 of file allpaths.c.

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

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

1707 {
1708  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
1709  rel->rows = 0;
1710  rel->reltarget->width = 0;
1711 
1712  /* Discard any pre-existing paths; no further need for them */
1713  rel->pathlist = NIL;
1714  rel->partial_pathlist = NIL;
1715 
1716  add_path(rel, (Path *) create_append_path(rel, NIL, NULL, 0, NIL));
1717 
1718  /*
1719  * We set the cheapest path immediately, to ensure that IS_DUMMY_REL()
1720  * will recognize the relation as dummy if anyone asks. This is redundant
1721  * when we're called from set_rel_size(), but not when called from
1722  * elsewhere, and doing it twice is harmless anyway.
1723  */
1724  set_cheapest(rel);
1725 }
#define NIL
Definition: pg_list.h:69
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
List * partial_pathlist
Definition: relation.h:541
AppendPath * create_append_path(RelOptInfo *rel, List *subpaths, Relids required_outer, int parallel_workers, List *partitioned_rels)
Definition: pathnode.c:1203
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
double rows
Definition: relation.h:528
#define NULL
Definition: c.h:229
int width
Definition: relation.h:887
List * pathlist
Definition: relation.h:539
struct PathTarget * reltarget
Definition: relation.h:536
Definition: relation.h:948
static void set_foreign_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 841 of file allpaths.c.

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

Referenced by set_rel_pathlist().

842 {
843  /* Call the FDW's GetForeignPaths function to generate path(s) */
844  rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
845 }
struct FdwRoutine * fdwroutine
Definition: relation.h:576
GetForeignPaths_function GetForeignPaths
Definition: fdwapi.h:175
static void set_foreign_size ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 824 of file allpaths.c.

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

Referenced by set_rel_size().

825 {
826  /* Mark rel with estimated output rows, width, etc */
827  set_foreign_size_estimates(root, rel);
828 
829  /* Let FDW adjust the size estimates, if it can */
830  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
831 
832  /* ... but do not let it set the rows estimate to zero */
833  rel->rows = clamp_row_est(rel->rows);
834 }
struct FdwRoutine * fdwroutine
Definition: relation.h:576
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:174
double rows
Definition: relation.h:528
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4806
double clamp_row_est(double nrows)
Definition: costsize.c:173
static void set_function_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 1939 of file allpaths.c.

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

Referenced by set_rel_pathlist().

1940 {
1941  Relids required_outer;
1942  List *pathkeys = NIL;
1943 
1944  /*
1945  * We don't support pushing join clauses into the quals of a function
1946  * scan, but it could still have required parameterization due to LATERAL
1947  * refs in the function expression.
1948  */
1949  required_outer = rel->lateral_relids;
1950 
1951  /*
1952  * The result is considered unordered unless ORDINALITY was used, in which
1953  * case it is ordered by the ordinal column (the last one). See if we
1954  * care, by checking for uses of that Var in equivalence classes.
1955  */
1956  if (rte->funcordinality)
1957  {
1958  AttrNumber ordattno = rel->max_attr;
1959  Var *var = NULL;
1960  ListCell *lc;
1961 
1962  /*
1963  * Is there a Var for it in rel's targetlist? If not, the query did
1964  * not reference the ordinality column, or at least not in any way
1965  * that would be interesting for sorting.
1966  */
1967  foreach(lc, rel->reltarget->exprs)
1968  {
1969  Var *node = (Var *) lfirst(lc);
1970 
1971  /* checking varno/varlevelsup is just paranoia */
1972  if (IsA(node, Var) &&
1973  node->varattno == ordattno &&
1974  node->varno == rel->relid &&
1975  node->varlevelsup == 0)
1976  {
1977  var = node;
1978  break;
1979  }
1980  }
1981 
1982  /*
1983  * Try to build pathkeys for this Var with int8 sorting. We tell
1984  * build_expression_pathkey not to build any new equivalence class; if
1985  * the Var isn't already mentioned in some EC, it means that nothing
1986  * cares about the ordering.
1987  */
1988  if (var)
1989  pathkeys = build_expression_pathkey(root,
1990  (Expr *) var,
1991  NULL, /* below outer joins */
1993  rel->relids,
1994  false);
1995  }
1996 
1997  /* Generate appropriate path */
1998  add_path(rel, create_functionscan_path(root, rel,
1999  pathkeys, required_outer));
2000 }
#define NIL
Definition: pg_list.h:69
#define Int8LessOperator
Definition: pg_operator.h:181
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Relids nullable_relids, Oid opno, Relids rel, bool create_it)
Definition: pathkeys.c:553
Index varlevelsup
Definition: primnodes.h:173
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
AttrNumber varattno
Definition: primnodes.h:168
bool funcordinality
Definition: parsenodes.h:999
Definition: primnodes.h:163
Path * create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1797
Relids lateral_relids
Definition: relation.h:550
Relids relids
Definition: relation.h:525
Index relid
Definition: relation.h:553
Index varno
Definition: primnodes.h:166
List * exprs
Definition: relation.h:884
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber max_attr
Definition: relation.h:557
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:536
int16 AttrNumber
Definition: attnum.h:21
static void set_namedtuplestore_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2117 of file allpaths.c.

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

Referenced by set_rel_size().

2119 {
2120  Relids required_outer;
2121 
2122  /* Mark rel with estimated output rows, width, etc */
2124 
2125  /*
2126  * We don't support pushing join clauses into the quals of a tuplestore
2127  * scan, but it could still have required parameterization due to LATERAL
2128  * refs in its tlist.
2129  */
2130  required_outer = rel->lateral_relids;
2131 
2132  /* Generate appropriate path */
2133  add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
2134 
2135  /* Select cheapest path (pretty easy in this case...) */
2136  set_cheapest(rel);
2137 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
Relids lateral_relids
Definition: relation.h:550
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
void set_namedtuplestore_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4767
Path * create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1900
static void set_plain_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 687 of file allpaths.c.

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

Referenced by set_rel_pathlist().

688 {
689  Relids required_outer;
690 
691  /*
692  * We don't support pushing join clauses into the quals of a seqscan, but
693  * it could still have required parameterization due to LATERAL refs in
694  * its tlist.
695  */
696  required_outer = rel->lateral_relids;
697 
698  /* Consider sequential scan */
699  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
700 
701  /* If appropriate, consider parallel sequential scan */
702  if (rel->consider_parallel && required_outer == NULL)
703  create_plain_partial_paths(root, rel);
704 
705  /* Consider index scans */
706  create_index_paths(root, rel);
707 
708  /* Consider TID scans */
709  create_tidscan_paths(root, rel);
710 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
void create_index_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:232
Relids lateral_relids
Definition: relation.h:550
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:253
#define NULL
Definition: c.h:229
bool consider_parallel
Definition: relation.h:533
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:717
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:938
static void set_plain_rel_size ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 512 of file allpaths.c.

References check_index_predicates(), and set_baserel_size_estimates().

Referenced by set_rel_size().

513 {
514  /*
515  * Test any partial indexes of rel for applicability. We must do this
516  * first since partial unique indexes can affect size estimates.
517  */
518  check_index_predicates(root, rel);
519 
520  /* Mark rel with estimated output rows, width, etc */
521  set_baserel_size_estimates(root, rel);
522 }
void check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:2774
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4037
static void set_rel_consider_parallel ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 529 of file allpaths.c.

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

Referenced by set_append_rel_size(), and set_base_rel_sizes().

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

Definition at line 421 of file allpaths.c.

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

Referenced by set_append_rel_pathlist(), and set_base_rel_pathlists().

423 {
424  if (IS_DUMMY_REL(rel))
425  {
426  /* We already proved the relation empty, so nothing more to do */
427  }
428  else if (rte->inh)
429  {
430  /* It's an "append relation", process accordingly */
431  set_append_rel_pathlist(root, rel, rti, rte);
432  }
433  else
434  {
435  switch (rel->rtekind)
436  {
437  case RTE_RELATION:
438  if (rte->relkind == RELKIND_FOREIGN_TABLE)
439  {
440  /* Foreign table */
441  set_foreign_pathlist(root, rel, rte);
442  }
443  else if (rte->tablesample != NULL)
444  {
445  /* Sampled relation */
446  set_tablesample_rel_pathlist(root, rel, rte);
447  }
448  else
449  {
450  /* Plain relation */
451  set_plain_rel_pathlist(root, rel, rte);
452  }
453  break;
454  case RTE_SUBQUERY:
455  /* Subquery --- fully handled during set_rel_size */
456  break;
457  case RTE_FUNCTION:
458  /* RangeFunction */
459  set_function_pathlist(root, rel, rte);
460  break;
461  case RTE_TABLEFUNC:
462  /* Table Function */
463  set_tablefunc_pathlist(root, rel, rte);
464  break;
465  case RTE_VALUES:
466  /* Values list */
467  set_values_pathlist(root, rel, rte);
468  break;
469  case RTE_CTE:
470  /* CTE reference --- fully handled during set_rel_size */
471  break;
472  case RTE_NAMEDTUPLESTORE:
473  /* tuplestore reference --- fully handled during set_rel_size */
474  break;
475  default:
476  elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
477  break;
478  }
479  }
480 
481  /*
482  * If this is a baserel, consider gathering any partial paths we may have
483  * created for it. (If we tried to gather inheritance children, we could
484  * end up with a very large number of gather nodes, each trying to grab
485  * its own pool of workers, so don't do this for otherrels. Instead,
486  * we'll consider gathering partial paths for the parent appendrel.)
487  */
488  if (rel->reloptkind == RELOPT_BASEREL)
489  generate_gather_paths(root, rel);
490 
491  /*
492  * Allow a plugin to editorialize on the set of Paths for this base
493  * relation. It could add new paths (such as CustomPaths) by calling
494  * add_path(), or delete or modify paths added by the core code.
495  */
497  (*set_rel_pathlist_hook) (root, rel, rti, rte);
498 
499  /* Now find the cheapest of the paths for this rel */
500  set_cheapest(rel);
501 
502 #ifdef OPTIMIZER_DEBUG
503  debug_print_rel(root, rel);
504 #endif
505 }
RelOptKind reloptkind
Definition: relation.h:522
set_rel_pathlist_hook_type set_rel_pathlist_hook
Definition: allpaths.c:64
#define ERROR
Definition: elog.h:43
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:1208
static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:841
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:2199
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:687
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
RTEKind rtekind
Definition: relation.h:555
static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:776
static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2027
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:1939
static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2007
struct TableSampleClause * tablesample
Definition: parsenodes.h:962
static void set_rel_size ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

Definition at line 318 of file allpaths.c.

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

Referenced by set_append_rel_size(), and set_base_rel_sizes().

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

Definition at line 1762 of file allpaths.c.

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

Referenced by set_rel_size().

1764 {
1765  Query *parse = root->parse;
1766  Query *subquery = rte->subquery;
1767  Relids required_outer;
1768  pushdown_safety_info safetyInfo;
1769  double tuple_fraction;
1770  RelOptInfo *sub_final_rel;
1771  ListCell *lc;
1772 
1773  /*
1774  * Must copy the Query so that planning doesn't mess up the RTE contents
1775  * (really really need to fix the planner to not scribble on its input,
1776  * someday ... but see remove_unused_subquery_outputs to start with).
1777  */
1778  subquery = copyObject(subquery);
1779 
1780  /*
1781  * If it's a LATERAL subquery, it might contain some Vars of the current
1782  * query level, requiring it to be treated as parameterized, even though
1783  * we don't support pushing down join quals into subqueries.
1784  */
1785  required_outer = rel->lateral_relids;
1786 
1787  /*
1788  * Zero out result area for subquery_is_pushdown_safe, so that it can set
1789  * flags as needed while recursing. In particular, we need a workspace
1790  * for keeping track of unsafe-to-reference columns. unsafeColumns[i]
1791  * will be set TRUE if we find that output column i of the subquery is
1792  * unsafe to use in a pushed-down qual.
1793  */
1794  memset(&safetyInfo, 0, sizeof(safetyInfo));
1795  safetyInfo.unsafeColumns = (bool *)
1796  palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
1797 
1798  /*
1799  * If the subquery has the "security_barrier" flag, it means the subquery
1800  * originated from a view that must enforce row level security. Then we
1801  * must not push down quals that contain leaky functions. (Ideally this
1802  * would be checked inside subquery_is_pushdown_safe, but since we don't
1803  * currently pass the RTE to that function, we must do it here.)
1804  */
1805  safetyInfo.unsafeLeaky = rte->security_barrier;
1806 
1807  /*
1808  * If there are any restriction clauses that have been attached to the
1809  * subquery relation, consider pushing them down to become WHERE or HAVING
1810  * quals of the subquery itself. This transformation is useful because it
1811  * may allow us to generate a better plan for the subquery than evaluating
1812  * all the subquery output rows and then filtering them.
1813  *
1814  * There are several cases where we cannot push down clauses. Restrictions
1815  * involving the subquery are checked by subquery_is_pushdown_safe().
1816  * Restrictions on individual clauses are checked by
1817  * qual_is_pushdown_safe(). Also, we don't want to push down
1818  * pseudoconstant clauses; better to have the gating node above the
1819  * subquery.
1820  *
1821  * Non-pushed-down clauses will get evaluated as qpquals of the
1822  * SubqueryScan node.
1823  *
1824  * XXX Are there any cases where we want to make a policy decision not to
1825  * push down a pushable qual, because it'd result in a worse plan?
1826  */
1827  if (rel->baserestrictinfo != NIL &&
1828  subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
1829  {
1830  /* OK to consider pushing down individual quals */
1831  List *upperrestrictlist = NIL;
1832  ListCell *l;
1833 
1834  foreach(l, rel->baserestrictinfo)
1835  {
1836  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1837  Node *clause = (Node *) rinfo->clause;
1838 
1839  if (!rinfo->pseudoconstant &&
1840  qual_is_pushdown_safe(subquery, rti, clause, &safetyInfo))
1841  {
1842  /* Push it down */
1843  subquery_push_qual(subquery, rte, rti, clause);
1844  }
1845  else
1846  {
1847  /* Keep it in the upper query */
1848  upperrestrictlist = lappend(upperrestrictlist, rinfo);
1849  }
1850  }
1851  rel->baserestrictinfo = upperrestrictlist;
1852  /* We don't bother recomputing baserestrict_min_security */
1853  }
1854 
1855  pfree(safetyInfo.unsafeColumns);
1856 
1857  /*
1858  * The upper query might not use all the subquery's output columns; if
1859  * not, we can simplify.
1860  */
1861  remove_unused_subquery_outputs(subquery, rel);
1862 
1863  /*
1864  * We can safely pass the outer tuple_fraction down to the subquery if the
1865  * outer level has no joining, aggregation, or sorting to do. Otherwise
1866  * we'd better tell the subquery to plan for full retrieval. (XXX This
1867  * could probably be made more intelligent ...)
1868  */
1869  if (parse->hasAggs ||
1870  parse->groupClause ||
1871  parse->groupingSets ||
1872  parse->havingQual ||
1873  parse->distinctClause ||
1874  parse->sortClause ||
1875  has_multiple_baserels(root))
1876  tuple_fraction = 0.0; /* default case */
1877  else
1878  tuple_fraction = root->tuple_fraction;
1879 
1880  /* plan_params should not be in use in current query level */
1881  Assert(root->plan_params == NIL);
1882 
1883  /* Generate a subroot and Paths for the subquery */
1884  rel->subroot = subquery_planner(root->glob, subquery,
1885  root,
1886  false, tuple_fraction);
1887 
1888  /* Isolate the params needed by this specific subplan */
1889  rel->subplan_params = root->plan_params;
1890  root->plan_params = NIL;
1891 
1892  /*
1893  * It's possible that constraint exclusion proved the subquery empty. If
1894  * so, it's desirable to produce an unadorned dummy path so that we will
1895  * recognize appropriate optimizations at this query level.
1896  */
1897  sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
1898 
1899  if (IS_DUMMY_REL(sub_final_rel))
1900  {
1902  return;
1903  }
1904 
1905  /*
1906  * Mark rel with estimated output rows, width, etc. Note that we have to
1907  * do this before generating outer-query paths, else cost_subqueryscan is
1908  * not happy.
1909  */
1910  set_subquery_size_estimates(root, rel);
1911 
1912  /*
1913  * For each Path that subquery_planner produced, make a SubqueryScanPath
1914  * in the outer query.
1915  */
1916  foreach(lc, sub_final_rel->pathlist)
1917  {
1918  Path *subpath = (Path *) lfirst(lc);
1919  List *pathkeys;
1920 
1921  /* Convert subpath's pathkeys to outer representation */
1922  pathkeys = convert_subquery_pathkeys(root,
1923  rel,
1924  subpath->pathkeys,
1926 
1927  /* Generate outer path using this subpath */
1928  add_path(rel, (Path *)
1929  create_subqueryscan_path(root, rel, subpath,
1930  pathkeys, required_outer));
1931  }
1932 }
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4549
#define NIL
Definition: pg_list.h:69
PathTarget * pathtarget
Definition: relation.h:955
Query * parse
Definition: relation.h:155
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
SubqueryScanPath * create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1769
List * plan_params
Definition: relation.h:169
List * sortClause
Definition: parsenodes.h:156
List * baserestrictinfo
Definition: relation.h:585
bool hasAggs
Definition: parsenodes.h:123
bool pseudoconstant
Definition: relation.h:1755
List * groupingSets
Definition: parsenodes.h:148
Definition: nodes.h:509
List * make_tlist_from_pathtarget(PathTarget *target)
Definition: tlist.c:595
bool * unsafeColumns
Definition: allpaths.c:52
char bool
Definition: c.h:202
List * targetList
Definition: parsenodes.h:138
PlannerInfo * subroot
Definition: relation.h:567
Relids lateral_relids
Definition: relation.h:550
double tuple_fraction
Definition: relation.h:291
void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:1706
void pfree(void *pointer)
Definition: mcxt.c:950
List * distinctClause
Definition: parsenodes.h:154
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:919
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
Definition: pathkeys.c:607
PlannerGlobal * glob
Definition: relation.h:157
static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
Definition: allpaths.c:2930
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1747
static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2759
void * palloc0(Size size)
Definition: mcxt.c:878
bool security_barrier
Definition: parsenodes.h:968
List * pathkeys
Definition: relation.h:968
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
static bool has_multiple_baserels(PlannerInfo *root)
Definition: allpaths.c:1729
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2836
static int list_length(const List *l)
Definition: pg_list.h:89
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2499
Query * subquery
Definition: parsenodes.h:967
List * groupClause
Definition: parsenodes.h:146
List * pathlist
Definition: relation.h:539
#define copyObject(obj)
Definition: nodes.h:622
Node * havingQual
Definition: parsenodes.h:150
Definition: pg_list.h:45
List * subplan_params
Definition: relation.h:568
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:492
Definition: relation.h:948
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649
static void set_tablefunc_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2027 of file allpaths.c.

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

Referenced by set_rel_pathlist().

2028 {
2029  Relids required_outer;
2030 
2031  /*
2032  * We don't support pushing join clauses into the quals of a tablefunc
2033  * scan, but it could still have required parameterization due to LATERAL
2034  * refs in the function expression.
2035  */
2036  required_outer = rel->lateral_relids;
2037 
2038  /* Generate appropriate path */
2039  add_path(rel, create_tablefuncscan_path(root, rel,
2040  required_outer));
2041 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
Relids lateral_relids
Definition: relation.h:550
Path * create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1823
static void set_tablesample_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 776 of file allpaths.c.

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

Referenced by set_rel_pathlist().

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

Definition at line 736 of file allpaths.c.

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

Referenced by set_rel_size().

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

Definition at line 2007 of file allpaths.c.

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

Referenced by set_rel_pathlist().

2008 {
2009  Relids required_outer;
2010 
2011  /*
2012  * We don't support pushing join clauses into the quals of a values scan,
2013  * but it could still have required parameterization due to LATERAL refs
2014  * in the values expressions.
2015  */
2016  required_outer = rel->lateral_relids;
2017 
2018  /* Generate appropriate path */
2019  add_path(rel, create_valuesscan_path(root, rel, required_outer));
2020 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
Path * create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1849
Relids lateral_relids
Definition: relation.h:550
static void set_worktable_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 2147 of file allpaths.c.

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

Referenced by set_rel_size().

2148 {
2149  Path *ctepath;
2150  PlannerInfo *cteroot;
2151  Index levelsup;
2152  Relids required_outer;
2153 
2154  /*
2155  * We need to find the non-recursive term's path, which is in the plan
2156  * level that's processing the recursive UNION, which is one level *below*
2157  * where the CTE comes from.
2158  */
2159  levelsup = rte->ctelevelsup;
2160  if (levelsup == 0) /* shouldn't happen */
2161  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2162  levelsup--;
2163  cteroot = root;
2164  while (levelsup-- > 0)
2165  {
2166  cteroot = cteroot->parent_root;
2167  if (!cteroot) /* shouldn't happen */
2168  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2169  }
2170  ctepath = cteroot->non_recursive_path;
2171  if (!ctepath) /* shouldn't happen */
2172  elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
2173 
2174  /* Mark rel with estimated output rows, width, etc */
2175  set_cte_size_estimates(root, rel, ctepath->rows);
2176 
2177  /*
2178  * We don't support pushing join clauses into the quals of a worktable
2179  * scan, but it could still have required parameterization due to LATERAL
2180  * refs in its tlist. (I'm not sure this is actually possible given the
2181  * restrictions on recursive references, but it's easy enough to support.)
2182  */
2183  required_outer = rel->lateral_relids;
2184 
2185  /* Generate appropriate path */
2186  add_path(rel, create_worktablescan_path(root, rel, required_outer));
2187 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:4730
Relids lateral_relids
Definition: relation.h:550
#define ERROR
Definition: elog.h:43
Path * create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1926
struct PlannerInfo * parent_root
Definition: relation.h:161
unsigned int Index
Definition: c.h:365
double rows
Definition: relation.h:964
struct Path * non_recursive_path
Definition: relation.h:309
Index ctelevelsup
Definition: parsenodes.h:1015
char * ctename
Definition: parsenodes.h:1014
#define elog
Definition: elog.h:219
Definition: relation.h:948
RelOptInfo* standard_join_search ( PlannerInfo root,
int  levels_needed,
List initial_rels 
)

Definition at line 2351 of file allpaths.c.

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

Referenced by make_rel_from_joinlist().

2352 {
2353  int lev;
2354  RelOptInfo *rel;
2355 
2356  /*
2357  * This function cannot be invoked recursively within any one planning
2358  * problem, so join_rel_level[] can't be in use already.
2359  */
2360  Assert(root->join_rel_level == NULL);
2361 
2362  /*
2363  * We employ a simple "dynamic programming" algorithm: we first find all
2364  * ways to build joins of two jointree items, then all ways to build joins
2365  * of three items (from two-item joins and single items), then four-item
2366  * joins, and so on until we have considered all ways to join all the
2367  * items into one rel.
2368  *
2369  * root->join_rel_level[j] is a list of all the j-item rels. Initially we
2370  * set root->join_rel_level[1] to represent all the single-jointree-item
2371  * relations.
2372  */
2373  root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
2374 
2375  root->join_rel_level[1] = initial_rels;
2376 
2377  for (lev = 2; lev <= levels_needed; lev++)
2378  {
2379  ListCell *lc;
2380 
2381  /*
2382  * Determine all possible pairs of relations to be joined at this
2383  * level, and build paths for making each one from every available
2384  * pair of lower-level relations.
2385  */
2386  join_search_one_level(root, lev);
2387 
2388  /*
2389  * Run generate_gather_paths() for each just-processed joinrel. We
2390  * could not do this earlier because both regular and partial paths
2391  * can get added to a particular joinrel at multiple times within
2392  * join_search_one_level. After that, we're done creating paths for
2393  * the joinrel, so run set_cheapest().
2394  */
2395  foreach(lc, root->join_rel_level[lev])
2396  {
2397  rel = (RelOptInfo *) lfirst(lc);
2398 
2399  /* Create GatherPaths for any useful partial paths for rel */
2400  generate_gather_paths(root, rel);
2401 
2402  /* Find and save the cheapest paths for this rel */
2403  set_cheapest(rel);
2404 
2405 #ifdef OPTIMIZER_DEBUG
2406  debug_print_rel(root, rel);
2407 #endif
2408  }
2409  }
2410 
2411  /*
2412  * We should have a single rel at the final level.
2413  */
2414  if (root->join_rel_level[levels_needed] == NIL)
2415  elog(ERROR, "failed to build any %d-way joins", levels_needed);
2416  Assert(list_length(root->join_rel_level[levels_needed]) == 1);
2417 
2418  rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
2419 
2420  root->join_rel_level = NULL;
2421 
2422  return rel;
2423 }
#define NIL
Definition: pg_list.h:69
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:2199
void join_search_one_level(PlannerInfo *root, int level)
Definition: joinrels.c:54
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
List ** join_rel_level
Definition: relation.h:225
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static bool subquery_is_pushdown_safe ( Query subquery,
Query topquery,
pushdown_safety_info safetyInfo 
)
static

Definition at line 2499 of file allpaths.c.

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

Referenced by recurse_pushdown_safe(), and set_subquery_pathlist().

2501 {
2502  SetOperationStmt *topop;
2503 
2504  /* Check point 1 */
2505  if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
2506  return false;
2507 
2508  /* Check points 3, 4, and 5 */
2509  if (subquery->distinctClause ||
2510  subquery->hasWindowFuncs ||
2511  subquery->hasTargetSRFs)
2512  safetyInfo->unsafeVolatile = true;
2513 
2514  /*
2515  * If we're at a leaf query, check for unsafe expressions in its target
2516  * list, and mark any unsafe ones in unsafeColumns[]. (Non-leaf nodes in
2517  * setop trees have only simple Vars in their tlists, so no need to check
2518  * them.)
2519  */
2520  if (subquery->setOperations == NULL)
2521  check_output_expressions(subquery, safetyInfo);
2522 
2523  /* Are we at top level, or looking at a setop component? */
2524  if (subquery == topquery)
2525  {
2526  /* Top level, so check any component queries */
2527  if (subquery->setOperations != NULL)
2528  if (!recurse_pushdown_safe(subquery->setOperations, topquery,
2529  safetyInfo))
2530  return false;
2531  }
2532  else
2533  {
2534  /* Setop component must not have more components (too weird) */
2535  if (subquery->setOperations != NULL)
2536  return false;
2537  /* Check whether setop component output types match top level */
2538  topop = castNode(SetOperationStmt, topquery->setOperations);
2539  Assert(topop);
2541  topop->colTypes,
2542  safetyInfo);
2543  }
2544  return true;
2545 }
Node * limitOffset
Definition: parsenodes.h:158
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2551
static void compare_tlist_datatypes(List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2686
List * targetList
Definition: parsenodes.h:138
List * distinctClause
Definition: parsenodes.h:154
Node * limitCount
Definition: parsenodes.h:159
static void check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2618
bool hasTargetSRFs
Definition: parsenodes.h:125
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
bool hasWindowFuncs
Definition: parsenodes.h:124
Node * setOperations
Definition: parsenodes.h:163
static void subquery_push_qual ( Query subquery,
RangeTblEntry rte,
Index  rti,
Node qual 
)
static

Definition at line 2836 of file allpaths.c.

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

Referenced by recurse_push_qual(), and set_subquery_pathlist().

2837 {
2838  if (subquery->setOperations != NULL)
2839  {
2840  /* Recurse to push it separately to each component query */
2841  recurse_push_qual(subquery->setOperations, subquery,
2842  rte, rti, qual);
2843  }
2844  else
2845  {
2846  /*
2847  * We need to replace Vars in the qual (which must refer to outputs of
2848  * the subquery) with copies of the subquery's targetlist expressions.
2849  * Note that at this point, any uplevel Vars in the qual should have
2850  * been replaced with Params, so they need no work.
2851  *
2852  * This step also ensures that when we are pushing into a setop tree,
2853  * each component query gets its own copy of the qual.
2854  */
2855  qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
2856  subquery->targetList,
2858  &subquery->hasSubLinks);
2859 
2860  /*
2861  * Now attach the qual to the proper place: normally WHERE, but if the
2862  * subquery uses grouping or aggregation, put it in HAVING (since the
2863  * qual really refers to the group-result rows).
2864  */
2865  if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
2866  subquery->havingQual = make_and_qual(subquery->havingQual, qual);
2867  else
2868  subquery->jointree->quals =
2869  make_and_qual(subquery->jointree->quals, qual);
2870 
2871  /*
2872  * We need not change the subquery's hasAggs or hasSubLinks flags,
2873  * since we can't be pushing down any aggregates that weren't there
2874  * before, and we don't push down subselects at all.
2875  */
2876  }
2877 }
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: clauses.c:348
FromExpr * jointree
Definition: parsenodes.h:136
bool hasAggs
Definition: parsenodes.h:123
List * groupingSets
Definition: parsenodes.h:148
Node * quals
Definition: primnodes.h:1472
List * targetList
Definition: parsenodes.h:138
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
#define NULL
Definition: c.h:229
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2883
Node * setOperations
Definition: parsenodes.h:163
List * groupClause
Definition: parsenodes.h:146
bool hasSubLinks
Definition: parsenodes.h:126
Node * havingQual
Definition: parsenodes.h:150
static bool targetIsInAllPartitionLists ( TargetEntry tle,
Query query 
)
static

Definition at line 2719 of file allpaths.c.

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

Referenced by check_output_expressions().

2720 {
2721  ListCell *lc;
2722 
2723  foreach(lc, query->windowClause)
2724  {
2725  WindowClause *wc = (WindowClause *) lfirst(lc);
2726 
2728  return false;
2729  }
2730  return true;
2731 }
List * windowClause
Definition: parsenodes.h:152
List * partitionClause
Definition: parsenodes.h:1277
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
#define lfirst(lc)
Definition: pg_list.h:106

Variable Documentation

bool enable_geqo = false

Definition at line 58 of file allpaths.c.

Referenced by make_rel_from_joinlist().

int geqo_threshold

Definition at line 59 of file allpaths.c.

Referenced by make_rel_from_joinlist().

join_search_hook_type join_search_hook = NULL

Definition at line 67 of file allpaths.c.

Referenced by make_rel_from_joinlist().

int min_parallel_index_scan_size

Definition at line 61 of file allpaths.c.

Referenced by compute_parallel_worker().

int min_parallel_table_scan_size

Definition at line 60 of file allpaths.c.

Referenced by compute_parallel_worker().

set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL

Definition at line 64 of file allpaths.c.

Referenced by set_rel_pathlist().