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

1660 {
1661  if (IsA(path, AppendPath))
1662  {
1663  AppendPath *apath = (AppendPath *) path;
1664 
1665  /* list_copy is important here to avoid sharing list substructure */
1666  return list_concat(subpaths, list_copy(apath->subpaths));
1667  }
1668  else if (IsA(path, MergeAppendPath))
1669  {
1670  MergeAppendPath *mpath = (MergeAppendPath *) path;
1671 
1672  /* list_copy is important here to avoid sharing list substructure */
1673  return list_concat(subpaths, list_copy(mpath->subpaths));
1674  }
1675  else
1676  return lappend(subpaths, path);
1677 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
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:1144
List * lappend(List *list, void *datum)
Definition: list.c:128
List * subpaths
Definition: relation.h:1164
static void add_paths_to_append_rel ( PlannerInfo root,
RelOptInfo rel,
List live_childrels 
)
static

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

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

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

2572 {
2573  ListCell *lc;
2574 
2575  foreach(lc, subquery->targetList)
2576  {
2577  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2578 
2579  if (tle->resjunk)
2580  continue; /* ignore resjunk columns */
2581 
2582  /* We need not check further if output col is already known unsafe */
2583  if (safetyInfo->unsafeColumns[tle->resno])
2584  continue;
2585 
2586  /* Functions returning sets are unsafe (point 1) */
2587  if (subquery->hasTargetSRFs &&
2588  expression_returns_set((Node *) tle->expr))
2589  {
2590  safetyInfo->unsafeColumns[tle->resno] = true;
2591  continue;
2592  }
2593 
2594  /* Volatile functions are unsafe (point 2) */
2595  if (contain_volatile_functions((Node *) tle->expr))
2596  {
2597  safetyInfo->unsafeColumns[tle->resno] = true;
2598  continue;
2599  }
2600 
2601  /* If subquery uses DISTINCT ON, check point 3 */
2602  if (subquery->hasDistinctOn &&
2603  !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
2604  {
2605  /* non-DISTINCT column, so mark it unsafe */
2606  safetyInfo->unsafeColumns[tle->resno] = true;
2607  continue;
2608  }
2609 
2610  /* If subquery uses window functions, check point 4 */
2611  if (subquery->hasWindowFuncs &&
2612  !targetIsInAllPartitionLists(tle, subquery))
2613  {
2614  /* not present in all PARTITION BY clauses, so mark it unsafe */
2615  safetyInfo->unsafeColumns[tle->resno] = true;
2616  continue;
2617  }
2618  }
2619 }
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:667
Definition: nodes.h:522
bool * unsafeColumns
Definition: allpaths.c:52
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
bool hasDistinctOn
Definition: parsenodes.h:120
List * targetList
Definition: parsenodes.h:131
bool resjunk
Definition: primnodes.h:1359
List * distinctClause
Definition: parsenodes.h:145
static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
Definition: allpaths.c:2672
AttrNumber resno
Definition: primnodes.h:1353
#define InvalidOid
Definition: postgres_ext.h:36
bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
bool hasTargetSRFs
Definition: parsenodes.h:118
#define lfirst(lc)
Definition: pg_list.h:106
bool hasWindowFuncs
Definition: parsenodes.h:117
Expr * expr
Definition: primnodes.h:1352
static void compare_tlist_datatypes ( List tlist,
List colTypes,
pushdown_safety_info safetyInfo 
)
static

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

2641 {
2642  ListCell *l;
2643  ListCell *colType = list_head(colTypes);
2644 
2645  foreach(l, tlist)
2646  {
2647  TargetEntry *tle = (TargetEntry *) lfirst(l);
2648 
2649  if (tle->resjunk)
2650  continue; /* ignore resjunk columns */
2651  if (colType == NULL)
2652  elog(ERROR, "wrong number of tlist entries");
2653  if (exprType((Node *) tle->expr) != lfirst_oid(colType))
2654  safetyInfo->unsafeColumns[tle->resno] = true;
2655  colType = lnext(colType);
2656  }
2657  if (colType != NULL)
2658  elog(ERROR, "wrong number of tlist entries");
2659 }
Definition: nodes.h:522
bool * unsafeColumns
Definition: allpaths.c:52
bool resjunk
Definition: primnodes.h:1359
#define ERROR
Definition: elog.h:43
AttrNumber resno
Definition: primnodes.h:1353
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:1352
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 3020 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().

3021 {
3022  int parallel_workers = 0;
3023 
3024  /*
3025  * If the user has set the parallel_workers reloption, use that; otherwise
3026  * select a default number of workers.
3027  */
3028  if (rel->rel_parallel_workers != -1)
3029  parallel_workers = rel->rel_parallel_workers;
3030  else
3031  {
3032  /*
3033  * If the number of pages being scanned is insufficient to justify a
3034  * parallel scan, just return zero ... unless it's an inheritance
3035  * child. In that case, we want to generate a parallel path here
3036  * anyway. It might not be worthwhile just for this relation, but
3037  * when combined with all of its inheritance siblings it may well pay
3038  * off.
3039  */
3040  if (rel->reloptkind == RELOPT_BASEREL &&
3041  ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3042  (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3043  return 0;
3044 
3045  if (heap_pages >= 0)
3046  {
3047  int heap_parallel_threshold;
3048  int heap_parallel_workers = 1;
3049 
3050  /*
3051  * Select the number of workers based on the log of the size of
3052  * the relation. This probably needs to be a good deal more
3053  * sophisticated, but we need something here for now. Note that
3054  * the upper limit of the min_parallel_table_scan_size GUC is
3055  * chosen to prevent overflow here.
3056  */
3057  heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
3058  while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
3059  {
3060  heap_parallel_workers++;
3061  heap_parallel_threshold *= 3;
3062  if (heap_parallel_threshold > INT_MAX / 3)
3063  break; /* avoid overflow */
3064  }
3065 
3066  parallel_workers = heap_parallel_workers;
3067  }
3068 
3069  if (index_pages >= 0)
3070  {
3071  int index_parallel_workers = 1;
3072  int index_parallel_threshold;
3073 
3074  /* same calculation as for heap_pages above */
3075  index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
3076  while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
3077  {
3078  index_parallel_workers++;
3079  index_parallel_threshold *= 3;
3080  if (index_parallel_threshold > INT_MAX / 3)
3081  break; /* avoid overflow */
3082  }
3083 
3084  if (parallel_workers > 0)
3085  parallel_workers = Min(parallel_workers, index_parallel_workers);
3086  else
3087  parallel_workers = index_parallel_workers;
3088  }
3089  }
3090 
3091  /*
3092  * In no case use more than max_parallel_workers_per_gather workers.
3093  */
3094  parallel_workers = Min(parallel_workers, max_parallel_workers_per_gather);
3095 
3096  return parallel_workers;
3097 }
RelOptKind reloptkind
Definition: relation.h:491
#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:538
#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 2988 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().

2990 {
2991  int parallel_workers;
2992  double pages_fetched;
2993 
2994  /* Compute heap pages for bitmap heap scan */
2995  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
2996  NULL, NULL);
2997 
2998  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1);
2999 
3000  if (parallel_workers <= 0)
3001  return;
3002 
3003  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3004  bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3005 }
Relids lateral_relids
Definition: relation.h:519
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3020
#define NULL
Definition: c.h:229
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
Definition: relation.h:911
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:5019
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 702 of file allpaths.c.

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

Referenced by set_plain_rel_pathlist().

703 {
704  int parallel_workers;
705 
706  parallel_workers = compute_parallel_worker(rel, rel->pages, -1);
707 
708  /* If any limit was set to zero, the user doesn't want a parallel scan. */
709  if (parallel_workers <= 0)
710  return;
711 
712  /* Add an unordered partial path based on a parallel sequential scan. */
713  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
714 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
Definition: allpaths.c:3020
BlockNumber pages
Definition: relation.h:533
#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 2152 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().

2153 {
2154  Path *cheapest_partial_path;
2155  Path *simple_gather_path;
2156  ListCell *lc;
2157 
2158  /* If there are no partial paths, there's nothing to do here. */
2159  if (rel->partial_pathlist == NIL)
2160  return;
2161 
2162  /*
2163  * The output of Gather is always unsorted, so there's only one partial
2164  * path of interest: the cheapest one. That will be the one at the front
2165  * of partial_pathlist because of the way add_partial_path works.
2166  */
2167  cheapest_partial_path = linitial(rel->partial_pathlist);
2168  simple_gather_path = (Path *)
2169  create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2170  NULL, NULL);
2171  add_path(rel, simple_gather_path);
2172 
2173  /*
2174  * For each useful ordering, we can consider an order-preserving Gather
2175  * Merge.
2176  */
2177  foreach (lc, rel->partial_pathlist)
2178  {
2179  Path *subpath = (Path *) lfirst(lc);
2180  GatherMergePath *path;
2181 
2182  if (subpath->pathkeys == NIL)
2183  continue;
2184 
2185  path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2186  subpath->pathkeys, NULL, NULL);
2187  add_path(rel, &path->path);
2188  }
2189 }
#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:510
#define linitial(l)
Definition: pg_list.h:110
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:932
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
struct PathTarget * reltarget
Definition: relation.h:505
Definition: relation.h:911
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 1491 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().

1495 {
1496  ListCell *lcp;
1497 
1498  foreach(lcp, all_child_pathkeys)
1499  {
1500  List *pathkeys = (List *) lfirst(lcp);
1501  List *startup_subpaths = NIL;
1502  List *total_subpaths = NIL;
1503  bool startup_neq_total = false;
1504  ListCell *lcr;
1505 
1506  /* Select the child paths for this ordering... */
1507  foreach(lcr, live_childrels)
1508  {
1509  RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1510  Path *cheapest_startup,
1511  *cheapest_total;
1512 
1513  /* Locate the right paths, if they are available. */
1514  cheapest_startup =
1516  pathkeys,
1517  NULL,
1518  STARTUP_COST,
1519  false);
1520  cheapest_total =
1522  pathkeys,
1523  NULL,
1524  TOTAL_COST,
1525  false);
1526 
1527  /*
1528  * If we can't find any paths with the right order just use the
1529  * cheapest-total path; we'll have to sort it later.
1530  */
1531  if (cheapest_startup == NULL || cheapest_total == NULL)
1532  {
1533  cheapest_startup = cheapest_total =
1534  childrel->cheapest_total_path;
1535  /* Assert we do have an unparameterized path for this child */
1536  Assert(cheapest_total->param_info == NULL);
1537  }
1538 
1539  /*
1540  * Notice whether we actually have different paths for the
1541  * "cheapest" and "total" cases; frequently there will be no point
1542  * in two create_merge_append_path() calls.
1543  */
1544  if (cheapest_startup != cheapest_total)
1545  startup_neq_total = true;
1546 
1547  startup_subpaths =
1548  accumulate_append_subpath(startup_subpaths, cheapest_startup);
1549  total_subpaths =
1550  accumulate_append_subpath(total_subpaths, cheapest_total);
1551  }
1552 
1553  /* ... and build the MergeAppend paths */
1554  add_path(rel, (Path *) create_merge_append_path(root,
1555  rel,
1556  startup_subpaths,
1557  pathkeys,
1558  NULL,
1559  partitioned_rels));
1560  if (startup_neq_total)
1561  add_path(rel, (Path *) create_merge_append_path(root,
1562  rel,
1563  total_subpaths,
1564  pathkeys,
1565  NULL,
1566  partitioned_rels));
1567  }
1568 }
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:920
struct Path * cheapest_total_path
Definition: relation.h:512
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:1659
List * pathlist
Definition: relation.h:508
Definition: pg_list.h:45
Definition: relation.h:911
static Path * get_cheapest_parameterized_child_path ( PlannerInfo root,
RelOptInfo rel,
Relids  required_outer 
)
static

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

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

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

1713 {
1714  int num_base_rels = 0;
1715  Index rti;
1716 
1717  for (rti = 1; rti < root->simple_rel_array_size; rti++)
1718  {
1719  RelOptInfo *brel = root->simple_rel_array[rti];
1720 
1721  if (brel == NULL)
1722  continue;
1723 
1724  /* ignore RTEs that are "other rels" */
1725  if (brel->reloptkind == RELOPT_BASEREL)
1726  if (++num_base_rels > 1)
1727  return true;
1728  }
1729  return false;
1730 }
RelOptKind reloptkind
Definition: relation.h:491
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 143 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().

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

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

2200 {
2201  int levels_needed;
2202  List *initial_rels;
2203  ListCell *jl;
2204 
2205  /*
2206  * Count the number of child joinlist nodes. This is the depth of the
2207  * dynamic-programming algorithm we must employ to consider all ways of
2208  * joining the child nodes.
2209  */
2210  levels_needed = list_length(joinlist);
2211 
2212  if (levels_needed <= 0)
2213  return NULL; /* nothing to do? */
2214 
2215  /*
2216  * Construct a list of rels corresponding to the child joinlist nodes.
2217  * This may contain both base rels and rels constructed according to
2218  * sub-joinlists.
2219  */
2220  initial_rels = NIL;
2221  foreach(jl, joinlist)
2222  {
2223  Node *jlnode = (Node *) lfirst(jl);
2224  RelOptInfo *thisrel;
2225 
2226  if (IsA(jlnode, RangeTblRef))
2227  {
2228  int varno = ((RangeTblRef *) jlnode)->rtindex;
2229 
2230  thisrel = find_base_rel(root, varno);
2231  }
2232  else if (IsA(jlnode, List))
2233  {
2234  /* Recurse to handle subproblem */
2235  thisrel = make_rel_from_joinlist(root, (List *) jlnode);
2236  }
2237  else
2238  {
2239  elog(ERROR, "unrecognized joinlist node type: %d",
2240  (int) nodeTag(jlnode));
2241  thisrel = NULL; /* keep compiler quiet */
2242  }
2243 
2244  initial_rels = lappend(initial_rels, thisrel);
2245  }
2246 
2247  if (levels_needed == 1)
2248  {
2249  /*
2250  * Single joinlist node, so we're done.
2251  */
2252  return (RelOptInfo *) linitial(initial_rels);
2253  }
2254  else
2255  {
2256  /*
2257  * Consider the different orders in which we could join the rels,
2258  * using a plugin, GEQO, or the regular join search code.
2259  *
2260  * We put the initial_rels list into a PlannerInfo field because
2261  * has_legal_joinclause() needs to look at it (ugly :-().
2262  */
2263  root->initial_rels = initial_rels;
2264 
2265  if (join_search_hook)
2266  return (*join_search_hook) (root, levels_needed, initial_rels);
2267  else if (enable_geqo && levels_needed >= geqo_threshold)
2268  return geqo(root, levels_needed, initial_rels);
2269  else
2270  return standard_join_search(root, levels_needed, initial_rels);
2271  }
2272 }
RelOptInfo * standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
Definition: allpaths.c:2304
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
Definition: nodes.h:522
#define linitial(l)
Definition: pg_list.h:110
#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:2199
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:527
List * initial_rels
Definition: relation.h:268
#define elog
Definition: elog.h:219
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:223
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 2712 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().

2714 {
2715  bool safe = true;
2716  List *vars;
2717  ListCell *vl;
2718 
2719  /* Refuse subselects (point 1) */
2720  if (contain_subplans(qual))
2721  return false;
2722 
2723  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
2724  if (safetyInfo->unsafeVolatile &&
2726  return false;
2727 
2728  /* Refuse leaky quals if told to (point 3) */
2729  if (safetyInfo->unsafeLeaky &&
2730  contain_leaked_vars(qual))
2731  return false;
2732 
2733  /*
2734  * It would be unsafe to push down window function calls, but at least for
2735  * the moment we could never see any in a qual anyhow. (The same applies
2736  * to aggregates, which we check for in pull_var_clause below.)
2737  */
2739 
2740  /*
2741  * Examine all Vars used in clause; since it's a restriction clause, all
2742  * such Vars must refer to subselect output columns.
2743  */
2745  foreach(vl, vars)
2746  {
2747  Var *var = (Var *) lfirst(vl);
2748 
2749  /*
2750  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
2751  * It's not clear whether a PHV could safely be pushed down, and even
2752  * less clear whether such a situation could arise in any cases of
2753  * practical interest anyway. So for the moment, just refuse to push
2754  * down.
2755  */
2756  if (!IsA(var, Var))
2757  {
2758  safe = false;
2759  break;
2760  }
2761 
2762  Assert(var->varno == rti);
2763  Assert(var->varattno >= 0);
2764 
2765  /* Check point 4 */
2766  if (var->varattno == 0)
2767  {
2768  safe = false;
2769  break;
2770  }
2771 
2772  /* Check point 5 */
2773  if (safetyInfo->unsafeColumns[var->varattno])
2774  {
2775  safe = false;
2776  break;
2777  }
2778  }
2779 
2780  list_free(vars);
2781 
2782  return safe;
2783 }
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1427
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
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:950
Definition: primnodes.h:163
bool contain_subplans(Node *clause)
Definition: clauses.c:842
#define PVC_INCLUDE_PLACEHOLDERS
Definition: var.h:24
bool contain_window_function(Node *clause)
Definition: clauses.c:726
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 2836 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().

2838 {
2839  if (IsA(setOp, RangeTblRef))
2840  {
2841  RangeTblRef *rtr = (RangeTblRef *) setOp;
2842  RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
2843  Query *subquery = subrte->subquery;
2844 
2845  Assert(subquery != NULL);
2846  subquery_push_qual(subquery, rte, rti, qual);
2847  }
2848  else if (IsA(setOp, SetOperationStmt))
2849  {
2850  SetOperationStmt *op = (SetOperationStmt *) setOp;
2851 
2852  recurse_push_qual(op->larg, topquery, rte, rti, qual);
2853  recurse_push_qual(op->rarg, topquery, rte, rti, qual);
2854  }
2855  else
2856  {
2857  elog(ERROR, "unrecognized node type: %d",
2858  (int) nodeTag(setOp));
2859  }
2860 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
List * rtable
Definition: parsenodes.h:128
#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:2789
static void recurse_push_qual(Node *setOp, Query *topquery, RangeTblEntry *rte, Index rti, Node *qual)
Definition: allpaths.c:2836
#define nodeTag(nodeptr)
Definition: nodes.h:527
Query * subquery
Definition: parsenodes.h:934
#define elog
Definition: elog.h:219
static bool recurse_pushdown_safe ( Node setOp,
Query topquery,
pushdown_safety_info safetyInfo 
)
static

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

2506 {
2507  if (IsA(setOp, RangeTblRef))
2508  {
2509  RangeTblRef *rtr = (RangeTblRef *) setOp;
2510  RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
2511  Query *subquery = rte->subquery;
2512 
2513  Assert(subquery != NULL);
2514  return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
2515  }
2516  else if (IsA(setOp, SetOperationStmt))
2517  {
2518  SetOperationStmt *op = (SetOperationStmt *) setOp;
2519 
2520  /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
2521  if (op->op == SETOP_EXCEPT)
2522  return false;
2523  /* Else recurse */
2524  if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
2525  return false;
2526  if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
2527  return false;
2528  }
2529  else
2530  {
2531  elog(ERROR, "unrecognized node type: %d",
2532  (int) nodeTag(setOp));
2533  }
2534  return true;
2535 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2504
List * rtable
Definition: parsenodes.h:128
#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:1524
#define nodeTag(nodeptr)
Definition: nodes.h:527
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2452
Query * subquery
Definition: parsenodes.h:934
#define elog
Definition: elog.h:219
static void remove_unused_subquery_outputs ( Query subquery,
RelOptInfo rel 
)
static

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

2884 {
2885  Bitmapset *attrs_used = NULL;
2886  ListCell *lc;
2887 
2888  /*
2889  * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
2890  * could update all the child SELECTs' tlists, but it seems not worth the
2891  * trouble presently.
2892  */
2893  if (subquery->setOperations)
2894  return;
2895 
2896  /*
2897  * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
2898  * time: all its output columns must be used in the distinctClause.
2899  */
2900  if (subquery->distinctClause && !subquery->hasDistinctOn)
2901  return;
2902 
2903  /*
2904  * Collect a bitmap of all the output column numbers used by the upper
2905  * query.
2906  *
2907  * Add all the attributes needed for joins or final output. Note: we must
2908  * look at rel's targetlist, not the attr_needed data, because attr_needed
2909  * isn't computed for inheritance child rels, cf set_append_rel_size().
2910  * (XXX might be worth changing that sometime.)
2911  */
2912  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
2913 
2914  /* Add all the attributes used by un-pushed-down restriction clauses. */
2915  foreach(lc, rel->baserestrictinfo)
2916  {
2917  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2918 
2919  pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
2920  }
2921 
2922  /*
2923  * If there's a whole-row reference to the subquery, we can't remove
2924  * anything.
2925  */
2927  return;
2928 
2929  /*
2930  * Run through the tlist and zap entries we don't need. It's okay to
2931  * modify the tlist items in-place because set_subquery_pathlist made a
2932  * copy of the subquery.
2933  */
2934  foreach(lc, subquery->targetList)
2935  {
2936  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2937  Node *texpr = (Node *) tle->expr;
2938 
2939  /*
2940  * If it has a sortgroupref number, it's used in some sort/group
2941  * clause so we'd better not remove it. Also, don't remove any
2942  * resjunk columns, since their reason for being has nothing to do
2943  * with anybody reading the subquery's output. (It's likely that
2944  * resjunk columns in a sub-SELECT would always have ressortgroupref
2945  * set, but even if they don't, it seems imprudent to remove them.)
2946  */
2947  if (tle->ressortgroupref || tle->resjunk)
2948  continue;
2949 
2950  /*
2951  * If it's used by the upper query, we can't remove it.
2952  */
2954  attrs_used))
2955  continue;
2956 
2957  /*
2958  * If it contains a set-returning function, we can't remove it since
2959  * that could change the number of rows returned by the subquery.
2960  */
2961  if (subquery->hasTargetSRFs &&
2962  expression_returns_set(texpr))
2963  continue;
2964 
2965  /*
2966  * If it contains volatile functions, we daren't remove it for fear
2967  * that the user is expecting their side-effects to happen.
2968  */
2969  if (contain_volatile_functions(texpr))
2970  continue;
2971 
2972  /*
2973  * OK, we don't need it. Replace the expression with a NULL constant.
2974  * Preserve the exposed type of the expression, in case something
2975  * looks at the rowtype of the subquery's result.
2976  */
2977  tle->expr = (Expr *) makeNullConst(exprType(texpr),
2978  exprTypmod(texpr),
2979  exprCollation(texpr));
2980  }
2981 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
List * baserestrictinfo
Definition: relation.h:549
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:667
Definition: nodes.h:522
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:950
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:219
bool hasDistinctOn
Definition: parsenodes.h:120
List * targetList
Definition: parsenodes.h:131
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
bool resjunk
Definition: primnodes.h:1359
List * distinctClause
Definition: parsenodes.h:145
AttrNumber resno
Definition: primnodes.h:1353
Index relid
Definition: relation.h:522
Expr * clause
Definition: relation.h:1679
List * exprs
Definition: relation.h:847
bool hasTargetSRFs
Definition: parsenodes.h:118
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1352
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
Node * setOperations
Definition: parsenodes.h:154
Index ressortgroupref
Definition: primnodes.h:1355
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
struct PathTarget * reltarget
Definition: relation.h:505
static void set_append_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

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

1193 {
1194  int parentRTindex = rti;
1195  List *live_childrels = NIL;
1196  ListCell *l;
1197 
1198  /*
1199  * Generate access paths for each member relation, and remember the
1200  * non-dummy children.
1201  */
1202  foreach(l, root->append_rel_list)
1203  {
1204  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1205  int childRTindex;
1206  RangeTblEntry *childRTE;
1207  RelOptInfo *childrel;
1208 
1209  /* append_rel_list contains all append rels; ignore others */
1210  if (appinfo->parent_relid != parentRTindex)
1211  continue;
1212 
1213  /* Re-locate the child RTE and RelOptInfo */
1214  childRTindex = appinfo->child_relid;
1215  childRTE = root->simple_rte_array[childRTindex];
1216  childrel = root->simple_rel_array[childRTindex];
1217 
1218  /*
1219  * If set_append_rel_size() decided the parent appendrel was
1220  * parallel-unsafe at some point after visiting this child rel, we
1221  * need to propagate the unsafety marking down to the child, so that
1222  * we don't generate useless partial paths for it.
1223  */
1224  if (!rel->consider_parallel)
1225  childrel->consider_parallel = false;
1226 
1227  /*
1228  * Compute the child's access paths.
1229  */
1230  set_rel_pathlist(root, childrel, childRTindex, childRTE);
1231 
1232  /*
1233  * If child is dummy, ignore it.
1234  */
1235  if (IS_DUMMY_REL(childrel))
1236  continue;
1237 
1238  /*
1239  * Child is live, so add it to the live_childrels list for use below.
1240  */
1241  live_childrels = lappend(live_childrels, childrel);
1242  }
1243 
1244  /* Add paths to the "append" relation. */
1245  add_paths_to_append_rel(root, rel, live_childrels);
1246 }
#define NIL
Definition: pg_list.h:69
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
#define IS_DUMMY_REL(r)
Definition: relation.h:1151
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:416
#define lfirst(lc)
Definition: pg_list.h:106
bool consider_parallel
Definition: relation.h:502
static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1261
Index child_relid
Definition: relation.h:1908
Index parent_relid
Definition: relation.h:1907
Definition: pg_list.h:45
static void set_append_rel_size ( PlannerInfo root,
RelOptInfo rel,
Index  rti,
RangeTblEntry rte 
)
static

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

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

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

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

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

290 {
291  Index rti;
292 
293  for (rti = 1; rti < root->simple_rel_array_size; rti++)
294  {
295  RelOptInfo *rel = root->simple_rel_array[rti];
296 
297  /* there may be empty slots corresponding to non-baserel RTEs */
298  if (rel == NULL)
299  continue;
300 
301  Assert(rel->relid == rti); /* sanity check on array */
302 
303  /* ignore RTEs that are "other rels" */
304  if (rel->reloptkind != RELOPT_BASEREL)
305  continue;
306 
307  set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
308  }
309 }
RelOptKind reloptkind
Definition: relation.h:491
struct RelOptInfo ** simple_rel_array
Definition: relation.h:178
int simple_rel_array_size
Definition: relation.h:179
Index relid
Definition: relation.h:522
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:416
#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 246 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().

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

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

2035 {
2036  Plan *cteplan;
2037  PlannerInfo *cteroot;
2038  Index levelsup;
2039  int ndx;
2040  ListCell *lc;
2041  int plan_id;
2042  Relids required_outer;
2043 
2044  /*
2045  * Find the referenced CTE, and locate the plan previously made for it.
2046  */
2047  levelsup = rte->ctelevelsup;
2048  cteroot = root;
2049  while (levelsup-- > 0)
2050  {
2051  cteroot = cteroot->parent_root;
2052  if (!cteroot) /* shouldn't happen */
2053  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2054  }
2055 
2056  /*
2057  * Note: cte_plan_ids can be shorter than cteList, if we are still working
2058  * on planning the CTEs (ie, this is a side-reference from another CTE).
2059  * So we mustn't use forboth here.
2060  */
2061  ndx = 0;
2062  foreach(lc, cteroot->parse->cteList)
2063  {
2064  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2065 
2066  if (strcmp(cte->ctename, rte->ctename) == 0)
2067  break;
2068  ndx++;
2069  }
2070  if (lc == NULL) /* shouldn't happen */
2071  elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2072  if (ndx >= list_length(cteroot->cte_plan_ids))
2073  elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2074  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2075  Assert(plan_id > 0);
2076  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2077 
2078  /* Mark rel with estimated output rows, width, etc */
2079  set_cte_size_estimates(root, rel, cteplan->plan_rows);
2080 
2081  /*
2082  * We don't support pushing join clauses into the quals of a CTE scan, but
2083  * it could still have required parameterization due to LATERAL refs in
2084  * its tlist.
2085  */
2086  required_outer = rel->lateral_relids;
2087 
2088  /* Generate appropriate path */
2089  add_path(rel, create_ctescan_path(root, rel, required_outer));
2090 }
double plan_rows
Definition: plannodes.h:120
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:4655
Relids lateral_relids
Definition: relation.h:519
#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:982
List * cteList
Definition: parsenodes.h:126
char * ctename
Definition: parsenodes.h:981
#define elog
Definition: elog.h:219
Path * create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1875
void set_dummy_rel_pathlist ( RelOptInfo rel)

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

1690 {
1691  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
1692  rel->rows = 0;
1693  rel->reltarget->width = 0;
1694 
1695  /* Discard any pre-existing paths; no further need for them */
1696  rel->pathlist = NIL;
1697  rel->partial_pathlist = NIL;
1698 
1699  add_path(rel, (Path *) create_append_path(rel, NIL, NULL, 0, NIL));
1700 
1701  /*
1702  * We set the cheapest path immediately, to ensure that IS_DUMMY_REL()
1703  * will recognize the relation as dummy if anyone asks. This is redundant
1704  * when we're called from set_rel_size(), but not when called from
1705  * elsewhere, and doing it twice is harmless anyway.
1706  */
1707  set_cheapest(rel);
1708 }
#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:510
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:497
#define NULL
Definition: c.h:229
int width
Definition: relation.h:850
List * pathlist
Definition: relation.h:508
struct PathTarget * reltarget
Definition: relation.h:505
Definition: relation.h:911
static void set_foreign_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 826 of file allpaths.c.

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

Referenced by set_rel_pathlist().

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

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

810 {
811  /* Mark rel with estimated output rows, width, etc */
812  set_foreign_size_estimates(root, rel);
813 
814  /* Let FDW adjust the size estimates, if it can */
815  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
816 
817  /* ... but do not let it set the rows estimate to zero */
818  rel->rows = clamp_row_est(rel->rows);
819 }
struct FdwRoutine * fdwroutine
Definition: relation.h:545
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:174
double rows
Definition: relation.h:497
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4698
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 1922 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().

1923 {
1924  Relids required_outer;
1925  List *pathkeys = NIL;
1926 
1927  /*
1928  * We don't support pushing join clauses into the quals of a function
1929  * scan, but it could still have required parameterization due to LATERAL
1930  * refs in the function expression.
1931  */
1932  required_outer = rel->lateral_relids;
1933 
1934  /*
1935  * The result is considered unordered unless ORDINALITY was used, in which
1936  * case it is ordered by the ordinal column (the last one). See if we
1937  * care, by checking for uses of that Var in equivalence classes.
1938  */
1939  if (rte->funcordinality)
1940  {
1941  AttrNumber ordattno = rel->max_attr;
1942  Var *var = NULL;
1943  ListCell *lc;
1944 
1945  /*
1946  * Is there a Var for it in rel's targetlist? If not, the query did
1947  * not reference the ordinality column, or at least not in any way
1948  * that would be interesting for sorting.
1949  */
1950  foreach(lc, rel->reltarget->exprs)
1951  {
1952  Var *node = (Var *) lfirst(lc);
1953 
1954  /* checking varno/varlevelsup is just paranoia */
1955  if (IsA(node, Var) &&
1956  node->varattno == ordattno &&
1957  node->varno == rel->relid &&
1958  node->varlevelsup == 0)
1959  {
1960  var = node;
1961  break;
1962  }
1963  }
1964 
1965  /*
1966  * Try to build pathkeys for this Var with int8 sorting. We tell
1967  * build_expression_pathkey not to build any new equivalence class; if
1968  * the Var isn't already mentioned in some EC, it means that nothing
1969  * cares about the ordering.
1970  */
1971  if (var)
1972  pathkeys = build_expression_pathkey(root,
1973  (Expr *) var,
1974  NULL, /* below outer joins */
1976  rel->relids,
1977  false);
1978  }
1979 
1980  /* Generate appropriate path */
1981  add_path(rel, create_functionscan_path(root, rel,
1982  pathkeys, required_outer));
1983 }
#define NIL
Definition: pg_list.h:69
#define Int8LessOperator
Definition: pg_operator.h:181
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
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:966
Definition: primnodes.h:163
Path * create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1797
Relids lateral_relids
Definition: relation.h:519
Relids relids
Definition: relation.h:494
Index relid
Definition: relation.h:522
Index varno
Definition: primnodes.h:166
List * exprs
Definition: relation.h:847
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber max_attr
Definition: relation.h:526
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:505
int16 AttrNumber
Definition: attnum.h:21
static void set_plain_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

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

673 {
674  Relids required_outer;
675 
676  /*
677  * We don't support pushing join clauses into the quals of a seqscan, but
678  * it could still have required parameterization due to LATERAL refs in
679  * its tlist.
680  */
681  required_outer = rel->lateral_relids;
682 
683  /* Consider sequential scan */
684  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
685 
686  /* If appropriate, consider parallel sequential scan */
687  if (rel->consider_parallel && required_outer == NULL)
688  create_plain_partial_paths(root, rel);
689 
690  /* Consider index scans */
691  create_index_paths(root, rel);
692 
693  /* Consider TID scans */
694  create_tidscan_paths(root, rel);
695 }
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:519
void create_tidscan_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: tidpath.c:253
#define NULL
Definition: c.h:229
bool consider_parallel
Definition: relation.h:502
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:702
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 504 of file allpaths.c.

References check_index_predicates(), and set_baserel_size_estimates().

Referenced by set_rel_size().

505 {
506  /*
507  * Test any partial indexes of rel for applicability. We must do this
508  * first since partial unique indexes can affect size estimates.
509  */
510  check_index_predicates(root, rel);
511 
512  /* Mark rel with estimated output rows, width, etc */
513  set_baserel_size_estimates(root, rel);
514 }
void check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
Definition: indxpath.c:2774
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:3925
static void set_rel_consider_parallel ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 521 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(), FdwRoutine::IsForeignScanParallelSafe, NULL, PlannerGlobal::parallelModeOK, PROPARALLEL_SAFE, RangeTblEntry::relid, RangeTblEntry::relkind, RELKIND_FOREIGN_TABLE, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RELPERSISTENCE_TEMP, RelOptInfo::reltarget, RTE_CTE, RTE_FUNCTION, RTE_JOIN, 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().

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

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

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

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

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

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

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

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

Referenced by set_rel_pathlist().

2011 {
2012  Relids required_outer;
2013 
2014  /*
2015  * We don't support pushing join clauses into the quals of a tablefunc
2016  * scan, but it could still have required parameterization due to LATERAL
2017  * refs in the function expression.
2018  */
2019  required_outer = rel->lateral_relids;
2020 
2021  /* Generate appropriate path */
2022  add_path(rel, create_tablefuncscan_path(root, rel,
2023  required_outer));
2024 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
Relids lateral_relids
Definition: relation.h:519
Path * create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1823
static void set_tablesample_rel_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

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

762 {
763  Relids required_outer;
764  Path *path;
765 
766  /*
767  * We don't support pushing join clauses into the quals of a samplescan,
768  * but it could still have required parameterization due to LATERAL refs
769  * in its tlist or TABLESAMPLE arguments.
770  */
771  required_outer = rel->lateral_relids;
772 
773  /* Consider sampled scan */
774  path = create_samplescan_path(root, rel, required_outer);
775 
776  /*
777  * If the sampling method does not support repeatable scans, we must avoid
778  * plans that would scan the rel multiple times. Ideally, we'd simply
779  * avoid putting the rel on the inside of a nestloop join; but adding such
780  * a consideration to the planner seems like a great deal of complication
781  * to support an uncommon usage of second-rate sampling methods. Instead,
782  * if there is a risk that the query might perform an unsafe join, just
783  * wrap the SampleScan in a Materialize node. We can check for joins by
784  * counting the membership of all_baserels (note that this correctly
785  * counts inheritance trees as single rels). If we're inside a subquery,
786  * we can't easily check whether a join might occur in the outer query, so
787  * just assume one is possible.
788  *
789  * GetTsmRoutine is relatively expensive compared to the other tests here,
790  * so check repeatable_across_scans last, even though that's a bit odd.
791  */
792  if ((root->query_level > 1 ||
795  {
796  path = (Path *) create_material_path(rel, path);
797  }
798 
799  add_path(rel, path);
800 
801  /* For the moment, at least, there are no other paths to consider */
802 }
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:519
Relids all_baserels
Definition: relation.h:195
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:604
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:929
Definition: relation.h:911
static void set_tablesample_rel_size ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

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

722 {
723  TableSampleClause *tsc = rte->tablesample;
724  TsmRoutine *tsm;
725  BlockNumber pages;
726  double tuples;
727 
728  /*
729  * Test any partial indexes of rel for applicability. We must do this
730  * first since partial unique indexes can affect size estimates.
731  */
732  check_index_predicates(root, rel);
733 
734  /*
735  * Call the sampling method's estimation function to estimate the number
736  * of pages it will read and the number of tuples it will return. (Note:
737  * we assume the function returns sane values.)
738  */
739  tsm = GetTsmRoutine(tsc->tsmhandler);
740  tsm->SampleScanGetSampleSize(root, rel, tsc->args,
741  &pages, &tuples);
742 
743  /*
744  * For the moment, because we will only consider a SampleScan path for the
745  * rel, it's okay to just overwrite the pages and tuples estimates for the
746  * whole relation. If we ever consider multiple path types for sampled
747  * rels, we'll need more complication.
748  */
749  rel->pages = pages;
750  rel->tuples = tuples;
751 
752  /* Mark rel with estimated output rows, width, etc */
753  set_baserel_size_estimates(root, rel);
754 }
double tuples
Definition: relation.h:534
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:3925
TsmRoutine * GetTsmRoutine(Oid tsmhandler)
Definition: tablesample.c:27
BlockNumber pages
Definition: relation.h:533
struct TableSampleClause * tablesample
Definition: parsenodes.h:929
static void set_values_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 1990 of file allpaths.c.

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

Referenced by set_rel_pathlist().

1991 {
1992  Relids required_outer;
1993 
1994  /*
1995  * We don't support pushing join clauses into the quals of a values scan,
1996  * but it could still have required parameterization due to LATERAL refs
1997  * in the values expressions.
1998  */
1999  required_outer = rel->lateral_relids;
2000 
2001  /* Generate appropriate path */
2002  add_path(rel, create_valuesscan_path(root, rel, required_outer));
2003 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
Path * create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1849
Relids lateral_relids
Definition: relation.h:519
static void set_worktable_pathlist ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

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

2101 {
2102  Path *ctepath;
2103  PlannerInfo *cteroot;
2104  Index levelsup;
2105  Relids required_outer;
2106 
2107  /*
2108  * We need to find the non-recursive term's path, which is in the plan
2109  * level that's processing the recursive UNION, which is one level *below*
2110  * where the CTE comes from.
2111  */
2112  levelsup = rte->ctelevelsup;
2113  if (levelsup == 0) /* shouldn't happen */
2114  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2115  levelsup--;
2116  cteroot = root;
2117  while (levelsup-- > 0)
2118  {
2119  cteroot = cteroot->parent_root;
2120  if (!cteroot) /* shouldn't happen */
2121  elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2122  }
2123  ctepath = cteroot->non_recursive_path;
2124  if (!ctepath) /* shouldn't happen */
2125  elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
2126 
2127  /* Mark rel with estimated output rows, width, etc */
2128  set_cte_size_estimates(root, rel, ctepath->rows);
2129 
2130  /*
2131  * We don't support pushing join clauses into the quals of a worktable
2132  * scan, but it could still have required parameterization due to LATERAL
2133  * refs in its tlist. (I'm not sure this is actually possible given the
2134  * restrictions on recursive references, but it's easy enough to support.)
2135  */
2136  required_outer = rel->lateral_relids;
2137 
2138  /* Generate appropriate path */
2139  add_path(rel, create_worktablescan_path(root, rel, required_outer));
2140 }
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:4655
Relids lateral_relids
Definition: relation.h:519
#define ERROR
Definition: elog.h:43
Path * create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1900
struct PlannerInfo * parent_root
Definition: relation.h:160
unsigned int Index
Definition: c.h:365
double rows
Definition: relation.h:928
struct Path * non_recursive_path
Definition: relation.h:308
Index ctelevelsup
Definition: parsenodes.h:982
char * ctename
Definition: parsenodes.h:981
#define elog
Definition: elog.h:219
Definition: relation.h:911
RelOptInfo* standard_join_search ( PlannerInfo root,
int  levels_needed,
List initial_rels 
)

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

2305 {
2306  int lev;
2307  RelOptInfo *rel;
2308 
2309  /*
2310  * This function cannot be invoked recursively within any one planning
2311  * problem, so join_rel_level[] can't be in use already.
2312  */
2313  Assert(root->join_rel_level == NULL);
2314 
2315  /*
2316  * We employ a simple "dynamic programming" algorithm: we first find all
2317  * ways to build joins of two jointree items, then all ways to build joins
2318  * of three items (from two-item joins and single items), then four-item
2319  * joins, and so on until we have considered all ways to join all the
2320  * items into one rel.
2321  *
2322  * root->join_rel_level[j] is a list of all the j-item rels. Initially we
2323  * set root->join_rel_level[1] to represent all the single-jointree-item
2324  * relations.
2325  */
2326  root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
2327 
2328  root->join_rel_level[1] = initial_rels;
2329 
2330  for (lev = 2; lev <= levels_needed; lev++)
2331  {
2332  ListCell *lc;
2333 
2334  /*
2335  * Determine all possible pairs of relations to be joined at this
2336  * level, and build paths for making each one from every available
2337  * pair of lower-level relations.
2338  */
2339  join_search_one_level(root, lev);
2340 
2341  /*
2342  * Run generate_gather_paths() for each just-processed joinrel. We
2343  * could not do this earlier because both regular and partial paths
2344  * can get added to a particular joinrel at multiple times within
2345  * join_search_one_level. After that, we're done creating paths for
2346  * the joinrel, so run set_cheapest().
2347  */
2348  foreach(lc, root->join_rel_level[lev])
2349  {
2350  rel = (RelOptInfo *) lfirst(lc);
2351 
2352  /* Create GatherPaths for any useful partial paths for rel */
2353  generate_gather_paths(root, rel);
2354 
2355  /* Find and save the cheapest paths for this rel */
2356  set_cheapest(rel);
2357 
2358 #ifdef OPTIMIZER_DEBUG
2359  debug_print_rel(root, rel);
2360 #endif
2361  }
2362  }
2363 
2364  /*
2365  * We should have a single rel at the final level.
2366  */
2367  if (root->join_rel_level[levels_needed] == NIL)
2368  elog(ERROR, "failed to build any %d-way joins", levels_needed);
2369  Assert(list_length(root->join_rel_level[levels_needed]) == 1);
2370 
2371  rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
2372 
2373  root->join_rel_level = NULL;
2374 
2375  return rel;
2376 }
#define NIL
Definition: pg_list.h:69
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
void generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: allpaths.c:2152
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 2452 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().

2454 {
2455  SetOperationStmt *topop;
2456 
2457  /* Check point 1 */
2458  if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
2459  return false;
2460 
2461  /* Check points 3, 4, and 5 */
2462  if (subquery->distinctClause ||
2463  subquery->hasWindowFuncs ||
2464  subquery->hasTargetSRFs)
2465  safetyInfo->unsafeVolatile = true;
2466 
2467  /*
2468  * If we're at a leaf query, check for unsafe expressions in its target
2469  * list, and mark any unsafe ones in unsafeColumns[]. (Non-leaf nodes in
2470  * setop trees have only simple Vars in their tlists, so no need to check
2471  * them.)
2472  */
2473  if (subquery->setOperations == NULL)
2474  check_output_expressions(subquery, safetyInfo);
2475 
2476  /* Are we at top level, or looking at a setop component? */
2477  if (subquery == topquery)
2478  {
2479  /* Top level, so check any component queries */
2480  if (subquery->setOperations != NULL)
2481  if (!recurse_pushdown_safe(subquery->setOperations, topquery,
2482  safetyInfo))
2483  return false;
2484  }
2485  else
2486  {
2487  /* Setop component must not have more components (too weird) */
2488  if (subquery->setOperations != NULL)
2489  return false;
2490  /* Check whether setop component output types match top level */
2491  topop = castNode(SetOperationStmt, topquery->setOperations);
2492  Assert(topop);
2494  topop->colTypes,
2495  safetyInfo);
2496  }
2497  return true;
2498 }
Node * limitOffset
Definition: parsenodes.h:149
#define castNode(_type_, nodeptr)
Definition: nodes.h:591
static bool recurse_pushdown_safe(Node *setOp, Query *topquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2504
static void compare_tlist_datatypes(List *tlist, List *colTypes, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2639
List * targetList
Definition: parsenodes.h:131
List * distinctClause
Definition: parsenodes.h:145
Node * limitCount
Definition: parsenodes.h:150
static void check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
Definition: allpaths.c:2571
bool hasTargetSRFs
Definition: parsenodes.h:118
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool hasWindowFuncs
Definition: parsenodes.h:117
Node * setOperations
Definition: parsenodes.h:154
static void subquery_push_qual ( Query subquery,
RangeTblEntry rte,
Index  rti,
Node qual 
)
static

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

2790 {
2791  if (subquery->setOperations != NULL)
2792  {
2793  /* Recurse to push it separately to each component query */
2794  recurse_push_qual(subquery->setOperations, subquery,
2795  rte, rti, qual);
2796  }
2797  else
2798  {
2799  /*
2800  * We need to replace Vars in the qual (which must refer to outputs of
2801  * the subquery) with copies of the subquery's targetlist expressions.
2802  * Note that at this point, any uplevel Vars in the qual should have
2803  * been replaced with Params, so they need no work.
2804  *
2805  * This step also ensures that when we are pushing into a setop tree,
2806  * each component query gets its own copy of the qual.
2807  */
2808  qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
2809  subquery->targetList,
2811  &subquery->hasSubLinks);
2812 
2813  /*
2814  * Now attach the qual to the proper place: normally WHERE, but if the
2815  * subquery uses grouping or aggregation, put it in HAVING (since the
2816  * qual really refers to the group-result rows).
2817  */
2818  if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
2819  subquery->havingQual = make_and_qual(subquery->havingQual, qual);
2820  else
2821  subquery->jointree->quals =
2822  make_and_qual(subquery->jointree->quals, qual);
2823 
2824  /*
2825  * We need not change the subquery's hasAggs or hasSubLinks flags,
2826  * since we can't be pushing down any aggregates that weren't there
2827  * before, and we don't push down subselects at all.
2828  */
2829  }
2830 }
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: clauses.c:347
FromExpr * jointree
Definition: parsenodes.h:129
bool hasAggs
Definition: parsenodes.h:116
List * groupingSets
Definition: parsenodes.h:139
Node * quals
Definition: primnodes.h:1456
List * targetList
Definition: parsenodes.h:131
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:2836
Node * setOperations
Definition: parsenodes.h:154
List * groupClause
Definition: parsenodes.h:137
bool hasSubLinks
Definition: parsenodes.h:119
Node * havingQual
Definition: parsenodes.h:141
static bool targetIsInAllPartitionLists ( TargetEntry tle,
Query query 
)
static

Definition at line 2672 of file allpaths.c.

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

Referenced by check_output_expressions().

2673 {
2674  ListCell *lc;
2675 
2676  foreach(lc, query->windowClause)
2677  {
2678  WindowClause *wc = (WindowClause *) lfirst(lc);
2679 
2681  return false;
2682  }
2683  return true;
2684 }
List * windowClause
Definition: parsenodes.h:143
List * partitionClause
Definition: parsenodes.h:1234
#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().