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

1676 {
1677  if (IsA(path, AppendPath))
1678  {
1679  AppendPath *apath = (AppendPath *) path;
1680 
1681  /* list_copy is important here to avoid sharing list substructure */
1682  return list_concat(subpaths, list_copy(apath->subpaths));
1683  }
1684  else if (IsA(path, MergeAppendPath))
1685  {
1686  MergeAppendPath *mpath = (MergeAppendPath *) path;
1687 
1688  /* list_copy is important here to avoid sharing list substructure */
1689  return list_concat(subpaths, list_copy(mpath->subpaths));
1690  }
1691  else
1692  return lappend(subpaths, path);
1693 }
#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:1179
List * lappend(List *list, void *datum)
Definition: list.c:128
List * subpaths
Definition: relation.h:1199
static void add_paths_to_append_rel ( PlannerInfo root,
RelOptInfo rel,
List live_childrels 
)
static

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

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

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

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

2687 {
2688  ListCell *l;
2689  ListCell *colType = list_head(colTypes);
2690 
2691  foreach(l, tlist)
2692  {
2693  TargetEntry *tle = (TargetEntry *) lfirst(l);
2694 
2695  if (tle->resjunk)
2696  continue; /* ignore resjunk columns */
2697  if (colType == NULL)
2698  elog(ERROR, "wrong number of tlist entries");
2699  if (exprType((Node *) tle->expr) != lfirst_oid(colType))
2700  safetyInfo->unsafeColumns[tle->resno] = true;
2701  colType = lnext(colType);
2702  }
2703  if (colType != NULL)
2704  elog(ERROR, "wrong number of tlist entries");
2705 }
Definition: nodes.h:509
bool * unsafeColumns
Definition: allpaths.c:52
bool resjunk
Definition: primnodes.h:1374
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1368
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:1367
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 3066 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().

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

3036 {
3037  int parallel_workers;
3038  double pages_fetched;
3039 
3040  /* Compute heap pages for bitmap heap scan */
3041  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
3042  NULL, NULL);
3043 
3044  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1);
3045 
3046  if (parallel_workers <= 0)
3047  return;
3048 
3049  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3050  bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3051 }
Relids lateral_relids
Definition: relation.h:549
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3066
#define NULL
Definition: c.h:229
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
Definition: relation.h:946
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:5120
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 716 of file allpaths.c.

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

Referenced by set_plain_rel_pathlist().

717 {
718  int parallel_workers;
719 
720  parallel_workers = compute_parallel_worker(rel, rel->pages, -1);
721 
722  /* If any limit was set to zero, the user doesn't want a parallel scan. */
723  if (parallel_workers <= 0)
724  return;
725 
726  /* Add an unordered partial path based on a parallel sequential scan. */
727  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
728 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3066
BlockNumber pages
Definition: relation.h:563
#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 2198 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().

2199 {
2200  Path *cheapest_partial_path;
2201  Path *simple_gather_path;
2202  ListCell *lc;
2203 
2204  /* If there are no partial paths, there's nothing to do here. */
2205  if (rel->partial_pathlist == NIL)
2206  return;
2207 
2208  /*
2209  * The output of Gather is always unsorted, so there's only one partial
2210  * path of interest: the cheapest one. That will be the one at the front
2211  * of partial_pathlist because of the way add_partial_path works.
2212  */
2213  cheapest_partial_path = linitial(rel->partial_pathlist);
2214  simple_gather_path = (Path *)
2215  create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2216  NULL, NULL);
2217  add_path(rel, simple_gather_path);
2218 
2219  /*
2220  * For each useful ordering, we can consider an order-preserving Gather
2221  * Merge.
2222  */
2223  foreach (lc, rel->partial_pathlist)
2224  {
2225  Path *subpath = (Path *) lfirst(lc);
2226  GatherMergePath *path;
2227 
2228  if (subpath->pathkeys == NIL)
2229  continue;
2230 
2231  path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2232  subpath->pathkeys, NULL, NULL);
2233  add_path(rel, &path->path);
2234  }
2235 }
#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:1731
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
List * partial_pathlist
Definition: relation.h:540
#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:1640
List * pathkeys
Definition: relation.h:967
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
struct PathTarget * reltarget
Definition: relation.h:535
Definition: relation.h:946
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 1507 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().

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

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

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

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

1729 {
1730  int num_base_rels = 0;
1731  Index rti;
1732 
1733  for (rti = 1; rti < root->simple_rel_array_size; rti++)
1734  {
1735  RelOptInfo *brel = root->simple_rel_array[rti];
1736 
1737  if (brel == NULL)
1738  continue;
1739 
1740  /* ignore RTEs that are "other rels" */
1741  if (brel->reloptkind == RELOPT_BASEREL)
1742  if (++num_base_rels > 1)
1743  return true;
1744  }
1745  return false;
1746 }
RelOptKind reloptkind
Definition: relation.h:521
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
int simple_rel_array_size
Definition: relation.h:179
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:521
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:178
Relids all_baserels
Definition: relation.h:195
static RelOptInfo * make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:2245
Relids relids
Definition: relation.h:524
int simple_rel_array_size
Definition: relation.h:179
Index relid
Definition: relation.h:552
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:675
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 2245 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().

2246 {
2247  int levels_needed;
2248  List *initial_rels;
2249  ListCell *jl;
2250 
2251  /*
2252  * Count the number of child joinlist nodes. This is the depth of the
2253  * dynamic-programming algorithm we must employ to consider all ways of
2254  * joining the child nodes.
2255  */
2256  levels_needed = list_length(joinlist);
2257 
2258  if (levels_needed <= 0)
2259  return NULL; /* nothing to do? */
2260 
2261  /*
2262  * Construct a list of rels corresponding to the child joinlist nodes.
2263  * This may contain both base rels and rels constructed according to
2264  * sub-joinlists.
2265  */
2266  initial_rels = NIL;
2267  foreach(jl, joinlist)
2268  {
2269  Node *jlnode = (Node *) lfirst(jl);
2270  RelOptInfo *thisrel;
2271 
2272  if (IsA(jlnode, RangeTblRef))
2273  {
2274  int varno = ((RangeTblRef *) jlnode)->rtindex;
2275 
2276  thisrel = find_base_rel(root, varno);
2277  }
2278  else if (IsA(jlnode, List))
2279  {
2280  /* Recurse to handle subproblem */
2281  thisrel = make_rel_from_joinlist(root, (List *) jlnode);
2282  }
2283  else
2284  {
2285  elog(ERROR, "unrecognized joinlist node type: %d",
2286  (int) nodeTag(jlnode));
2287  thisrel = NULL; /* keep compiler quiet */
2288  }
2289 
2290  initial_rels = lappend(initial_rels, thisrel);
2291  }
2292 
2293  if (levels_needed == 1)
2294  {
2295  /*
2296  * Single joinlist node, so we're done.
2297  */
2298  return (RelOptInfo *) linitial(initial_rels);
2299  }
2300  else
2301  {
2302  /*
2303  * Consider the different orders in which we could join the rels,
2304  * using a plugin, GEQO, or the regular join search code.
2305  *
2306  * We put the initial_rels list into a PlannerInfo field because
2307  * has_legal_joinclause() needs to look at it (ugly :-().
2308  */
2309  root->initial_rels = initial_rels;
2310 
2311  if (join_search_hook)
2312  return (*join_search_hook) (root, levels_needed, initial_rels);
2313  else if (enable_geqo && levels_needed >= geqo_threshold)
2314  return geqo(root, levels_needed, initial_rels);
2315  else
2316  return standard_join_search(root, levels_needed, initial_rels);
2317  }
2318 }
RelOptInfo * standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
Definition: allpaths.c:2350
#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:2245
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:268
#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 2758 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().

2760 {
2761  bool safe = true;
2762  List *vars;
2763  ListCell *vl;
2764 
2765  /* Refuse subselects (point 1) */
2766  if (contain_subplans(qual))
2767  return false;
2768 
2769  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
2770  if (safetyInfo->unsafeVolatile &&
2772  return false;
2773 
2774  /* Refuse leaky quals if told to (point 3) */
2775  if (safetyInfo->unsafeLeaky &&
2776  contain_leaked_vars(qual))
2777  return false;
2778 
2779  /*
2780  * It would be unsafe to push down window function calls, but at least for
2781  * the moment we could never see any in a qual anyhow. (The same applies
2782  * to aggregates, which we check for in pull_var_clause below.)
2783  */
2785 
2786  /*
2787  * Examine all Vars used in clause; since it's a restriction clause, all
2788  * such Vars must refer to subselect output columns.
2789  */
2791  foreach(vl, vars)
2792  {
2793  Var *var = (Var *) lfirst(vl);
2794 
2795  /*
2796  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
2797  * It's not clear whether a PHV could safely be pushed down, and even
2798  * less clear whether such a situation could arise in any cases of
2799  * practical interest anyway. So for the moment, just refuse to push
2800  * down.
2801  */
2802  if (!IsA(var, Var))
2803  {
2804  safe = false;
2805  break;
2806  }
2807 
2808  Assert(var->varno == rti);
2809  Assert(var->varattno >= 0);
2810 
2811  /* Check point 4 */
2812  if (var->varattno == 0)
2813  {
2814  safe = false;
2815  break;
2816  }
2817 
2818  /* Check point 5 */
2819  if (safetyInfo->unsafeColumns[var->varattno])
2820  {
2821  safe = false;
2822  break;
2823  }
2824  }
2825 
2826  list_free(vars);
2827 
2828  return safe;
2829 }
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1461
#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:951
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:675
#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 2882 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().

2884 {
2885  if (IsA(setOp, RangeTblRef))
2886  {
2887  RangeTblRef *rtr = (RangeTblRef *) setOp;
2888  RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
2889  Query *subquery = subrte->subquery;
2890 
2891  Assert(subquery != NULL);
2892  subquery_push_qual(subquery, rte, rti, qual);
2893  }
2894  else if (IsA(setOp, SetOperationStmt))
2895  {
2896  SetOperationStmt *op = (SetOperationStmt *) setOp;
2897 
2898  recurse_push_qual(op->larg, topquery, rte, rti, qual);
2899  recurse_push_qual(op->rarg, topquery, rte, rti, qual);
2900  }
2901  else
2902  {
2903  elog(ERROR, "unrecognized node type: %d",
2904  (int) nodeTag(setOp));
2905  }
2906 }
#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:675
static void subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2835
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2882
#define nodeTag(nodeptr)
Definition: nodes.h:514
Query * subquery
Definition: parsenodes.h:947
#define elog
Definition: elog.h:219
static bool recurse_pushdown_safe ( Node setOp,
Query topquery,
pushdown_safety_info safetyInfo 
)
static

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

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

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

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

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

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

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

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

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

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

1706 {
1707  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
1708  rel->rows = 0;
1709  rel->reltarget->width = 0;
1710 
1711  /* Discard any pre-existing paths; no further need for them */
1712  rel->pathlist = NIL;
1713  rel->partial_pathlist = NIL;
1714 
1715  add_path(rel, (Path *) create_append_path(rel, NIL, NULL, 0, NIL));
1716 
1717  /*
1718  * We set the cheapest path immediately, to ensure that IS_DUMMY_REL()
1719  * will recognize the relation as dummy if anyone asks. This is redundant
1720  * when we're called from set_rel_size(), but not when called from
1721  * elsewhere, and doing it twice is harmless anyway.
1722  */
1723  set_cheapest(rel);
1724 }
#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:540
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:527
#define NULL
Definition: c.h:229
int width
Definition: relation.h:885
List * pathlist
Definition: relation.h:538
struct PathTarget * reltarget
Definition: relation.h:535
Definition: relation.h:946
static void set_foreign_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 840 of file allpaths.c.

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

Referenced by set_rel_pathlist().

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

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

824 {
825  /* Mark rel with estimated output rows, width, etc */
826  set_foreign_size_estimates(root, rel);
827 
828  /* Let FDW adjust the size estimates, if it can */
829  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
830 
831  /* ... but do not let it set the rows estimate to zero */
832  rel->rows = clamp_row_est(rel->rows);
833 }
struct FdwRoutine * fdwroutine
Definition: relation.h:575
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:174
double rows
Definition: relation.h:527
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4799
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 1938 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().

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

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

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

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

687 {
688  Relids required_outer;
689 
690  /*
691  * We don't support pushing join clauses into the quals of a seqscan, but
692  * it could still have required parameterization due to LATERAL refs in
693  * its tlist.
694  */
695  required_outer = rel->lateral_relids;
696 
697  /* Consider sequential scan */
698  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
699 
700  /* If appropriate, consider parallel sequential scan */
701  if (rel->consider_parallel && required_outer == NULL)
702  create_plain_partial_paths(root, rel);
703 
704  /* Consider index scans */
705  create_index_paths(root, rel);
706 
707  /* Consider TID scans */
708  create_tidscan_paths(root, rel);
709 }
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:549
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:253
#define NULL
Definition: c.h:229
bool consider_parallel
Definition: relation.h:532
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:716
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:3993
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  * tuplestore cannot be shared, at least without more
653  * infrastructure to support that.
654  */
655  return;
656  }
657 
658  /*
659  * If there's anything in baserestrictinfo that's parallel-restricted, we
660  * give up on parallelizing access to this relation. We could consider
661  * instead postponing application of the restricted quals until we're
662  * above all the parallelism in the plan tree, but it's not clear that
663  * that would be a win in very many cases, and it might be tricky to make
664  * outer join clauses work correctly. It would likely break equivalence
665  * classes, too.
666  */
667  if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
668  return;
669 
670  /*
671  * Likewise, if the relation's outputs are not parallel-safe, give up.
672  * (Usually, they're just Vars, but sometimes they're not.)
673  */
674  if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
675  return;
676 
677  /* We have a winner. */
678  rel->consider_parallel = true;
679 }
List * baserestrictinfo
Definition: relation.h:583
Definition: nodes.h:509
List * values_lists
Definition: parsenodes.h:989
#define IS_SIMPLE_REL(rel)
Definition: relation.h:504
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1073
IsForeignScanParallelSafe_function IsForeignScanParallelSafe
Definition: fdwapi.h:224
bool parallelModeOK
Definition: relation.h:128
PlannerGlobal * glob
Definition: relation.h:156
struct FdwRoutine * fdwroutine
Definition: relation.h:575
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define PROPARALLEL_SAFE
Definition: pg_proc.h:5488
List * exprs
Definition: relation.h:882
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
List * functions
Definition: parsenodes.h:978
char func_parallel(Oid funcid)
Definition: lsyscache.c:1603
bool consider_parallel
Definition: relation.h:532
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:1852
RTEKind rtekind
Definition: parsenodes.h:929
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
struct PathTarget * reltarget
Definition: relation.h:535
struct TableSampleClause * tablesample
Definition: parsenodes.h:942
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:521
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:1186
static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:1207
static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:840
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:2198
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:686
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
RTEKind rtekind
Definition: relation.h:554
static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:775
static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2026
#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:1938
static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2006
struct TableSampleClause * tablesample
Definition: parsenodes.h:942
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:521
static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2050
static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:823
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:2116
static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:858
void set_dummy_rel_pathlist(RelOptInfo *rel)
Definition: allpaths.c:1705
#define ERROR
Definition: elog.h:43
#define IS_DUMMY_REL(r)
Definition: relation.h:1186
static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
Definition: allpaths.c:1761
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:735
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
bool self_reference
Definition: parsenodes.h:996
RTEKind rtekind
Definition: relation.h:554
double rows
Definition: relation.h:527
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4691
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:4664
void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4626
#define elog
Definition: elog.h:219
struct TableSampleClause * tablesample
Definition: parsenodes.h:942
static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: allpaths.c:2146
static void set_subquery_pathlist ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

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

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

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

Referenced by set_rel_pathlist().

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

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

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

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

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

Definition at line 2006 of file allpaths.c.

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

Referenced by set_rel_pathlist().

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

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

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

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

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

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

2836 {
2837  if (subquery->setOperations != NULL)
2838  {
2839  /* Recurse to push it separately to each component query */
2840  recurse_push_qual(subquery->setOperations, subquery,
2841  rte, rti, qual);
2842  }
2843  else
2844  {
2845  /*
2846  * We need to replace Vars in the qual (which must refer to outputs of
2847  * the subquery) with copies of the subquery's targetlist expressions.
2848  * Note that at this point, any uplevel Vars in the qual should have
2849  * been replaced with Params, so they need no work.
2850  *
2851  * This step also ensures that when we are pushing into a setop tree,
2852  * each component query gets its own copy of the qual.
2853  */
2854  qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
2855  subquery->targetList,
2857  &subquery->hasSubLinks);
2858 
2859  /*
2860  * Now attach the qual to the proper place: normally WHERE, but if the
2861  * subquery uses grouping or aggregation, put it in HAVING (since the
2862  * qual really refers to the group-result rows).
2863  */
2864  if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
2865  subquery->havingQual = make_and_qual(subquery->havingQual, qual);
2866  else
2867  subquery->jointree->quals =
2868  make_and_qual(subquery->jointree->quals, qual);
2869 
2870  /*
2871  * We need not change the subquery's hasAggs or hasSubLinks flags,
2872  * since we can't be pushing down any aggregates that weren't there
2873  * before, and we don't push down subselects at all.
2874  */
2875  }
2876 }
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:1471
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:2882
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 2718 of file allpaths.c.

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

Referenced by check_output_expressions().

2719 {
2720  ListCell *lc;
2721 
2722  foreach(lc, query->windowClause)
2723  {
2724  WindowClause *wc = (WindowClause *) lfirst(lc);
2725 
2727  return false;
2728  }
2729  return true;
2730 }
List * windowClause
Definition: parsenodes.h:152
List * partitionClause
Definition: parsenodes.h:1250
#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().