PostgreSQL Source Code  git master
optimizer.h File Reference
#include "nodes/parsenodes.h"
Include dependency graph for optimizer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define HAVE_PLANNERINFO_TYPEDEF   1
 
#define HAVE_INDEXOPTINFO_TYPEDEF   1
 
#define HAVE_SPECIALJOININFO_TYPEDEF   1
 
#define PVC_INCLUDE_AGGREGATES   0x0001 /* include Aggrefs in output list */
 
#define PVC_RECURSE_AGGREGATES   0x0002 /* recurse into Aggref arguments */
 
#define PVC_INCLUDE_WINDOWFUNCS   0x0004 /* include WindowFuncs in output list */
 
#define PVC_RECURSE_WINDOWFUNCS   0x0008 /* recurse into WindowFunc arguments */
 
#define PVC_INCLUDE_PLACEHOLDERS
 
#define PVC_RECURSE_PLACEHOLDERS
 

Typedefs

typedef struct PlannerInfo PlannerInfo
 
typedef struct IndexOptInfo IndexOptInfo
 
typedef struct SpecialJoinInfo SpecialJoinInfo
 

Enumerations

enum  ForceParallelMode { FORCE_PARALLEL_OFF, FORCE_PARALLEL_ON, FORCE_PARALLEL_REGRESS }
 

Functions

Selectivity clause_selectivity (PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
Selectivity clauselist_selectivity_simple (PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, Bitmapset *estimatedclauses)
 
Selectivity clauselist_selectivity (PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
 
double clamp_row_est (double nrows)
 
bool is_pseudo_constant_for_index (Node *expr, IndexOptInfo *index)
 
struct PlannedStmtplanner (Query *parse, const char *query_string, int cursorOptions, struct ParamListInfoData *boundParams)
 
Exprexpression_planner (Expr *expr)
 
Exprexpression_planner_with_deps (Expr *expr, List **relationOids, List **invalItems)
 
bool plan_cluster_use_sort (Oid tableOid, Oid indexOid)
 
int plan_create_index_workers (Oid tableOid, Oid indexOid)
 
void extract_query_dependencies (Node *query, List **relationOids, List **invalItems, bool *hasRowSecurity)
 
Nodenegate_clause (Node *node)
 
Exprcanonicalize_qual (Expr *qual, bool is_check)
 
bool contain_mutable_functions (Node *clause)
 
bool contain_volatile_functions (Node *clause)
 
bool contain_volatile_functions_not_nextval (Node *clause)
 
Nodeeval_const_expressions (PlannerInfo *root, Node *node)
 
Nodeestimate_expression_value (PlannerInfo *root, Node *node)
 
Exprevaluate_expr (Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
 
Listexpand_function_arguments (List *args, Oid result_type, struct HeapTupleData *func_tuple)
 
bool predicate_implied_by (List *predicate_list, List *clause_list, bool weak)
 
bool predicate_refuted_by (List *predicate_list, List *clause_list, bool weak)
 
int count_nonjunk_tlist_entries (List *tlist)
 
TargetEntryget_sortgroupref_tle (Index sortref, List *targetList)
 
TargetEntryget_sortgroupclause_tle (SortGroupClause *sgClause, List *targetList)
 
Nodeget_sortgroupclause_expr (SortGroupClause *sgClause, List *targetList)
 
Listget_sortgrouplist_exprs (List *sgClauses, List *targetList)
 
SortGroupClauseget_sortgroupref_clause (Index sortref, List *clauses)
 
SortGroupClauseget_sortgroupref_clause_noerr (Index sortref, List *clauses)
 
Bitmapsetpull_varnos (Node *node)
 
Bitmapsetpull_varnos_of_level (Node *node, int levelsup)
 
void pull_varattnos (Node *node, Index varno, Bitmapset **varattnos)
 
Listpull_vars_of_level (Node *node, int levelsup)
 
bool contain_var_clause (Node *node)
 
bool contain_vars_of_level (Node *node, int levelsup)
 
int locate_var_of_level (Node *node, int levelsup)
 
Listpull_var_clause (Node *node, int flags)
 
Nodeflatten_join_alias_vars (Query *query, Node *node)
 

Variables

PGDLLIMPORT double seq_page_cost
 
PGDLLIMPORT double random_page_cost
 
PGDLLIMPORT double cpu_tuple_cost
 
PGDLLIMPORT double cpu_index_tuple_cost
 
PGDLLIMPORT double cpu_operator_cost
 
PGDLLIMPORT double parallel_tuple_cost
 
PGDLLIMPORT double parallel_setup_cost
 
PGDLLIMPORT int effective_cache_size
 
int force_parallel_mode
 
bool parallel_leader_participation
 

Macro Definition Documentation

◆ HAVE_INDEXOPTINFO_TYPEDEF

#define HAVE_INDEXOPTINFO_TYPEDEF   1

Definition at line 41 of file optimizer.h.

◆ HAVE_PLANNERINFO_TYPEDEF

#define HAVE_PLANNERINFO_TYPEDEF   1

Definition at line 35 of file optimizer.h.

◆ HAVE_SPECIALJOININFO_TYPEDEF

#define HAVE_SPECIALJOININFO_TYPEDEF   1

Definition at line 45 of file optimizer.h.

◆ PVC_INCLUDE_AGGREGATES

#define PVC_INCLUDE_AGGREGATES   0x0001 /* include Aggrefs in output list */

◆ PVC_INCLUDE_PLACEHOLDERS

◆ PVC_INCLUDE_WINDOWFUNCS

#define PVC_INCLUDE_WINDOWFUNCS   0x0004 /* include WindowFuncs in output list */

◆ PVC_RECURSE_AGGREGATES

◆ PVC_RECURSE_PLACEHOLDERS

#define PVC_RECURSE_PLACEHOLDERS
Value:
0x0020 /* recurse into PlaceHolderVar
* arguments */

Definition at line 177 of file optimizer.h.

Referenced by build_tlist_to_deparse(), estimate_num_groups(), pull_var_clause(), and pull_var_clause_walker().

◆ PVC_RECURSE_WINDOWFUNCS

Typedef Documentation

◆ IndexOptInfo

typedef struct IndexOptInfo IndexOptInfo

Definition at line 40 of file optimizer.h.

◆ PlannerInfo

typedef struct PlannerInfo PlannerInfo

Definition at line 34 of file optimizer.h.

◆ SpecialJoinInfo

Definition at line 44 of file optimizer.h.

Enumeration Type Documentation

◆ ForceParallelMode

Enumerator
FORCE_PARALLEL_OFF 
FORCE_PARALLEL_ON 
FORCE_PARALLEL_REGRESS 

Definition at line 94 of file optimizer.h.

Function Documentation

◆ canonicalize_qual()

Expr* canonicalize_qual ( Expr qual,
bool  is_check 
)

Definition at line 292 of file prepqual.c.

References Assert, find_duplicate_ors(), and IsA.

Referenced by ConstraintImpliedByRelConstraint(), convert_EXISTS_to_ANY(), DoCopy(), get_proposed_default_constraint(), get_relation_constraints(), preprocess_expression(), and RelationGetIndexPredicate().

293 {
294  Expr *newqual;
295 
296  /* Quick exit for empty qual */
297  if (qual == NULL)
298  return NULL;
299 
300  /* This should not be invoked on quals in implicit-AND format */
301  Assert(!IsA(qual, List));
302 
303  /*
304  * Pull up redundant subclauses in OR-of-AND trees. We do this only
305  * within the top-level AND/OR structure; there's no point in looking
306  * deeper. Also remove any NULL constants in the top-level structure.
307  */
308  newqual = find_duplicate_ors(qual, is_check);
309 
310  return newqual;
311 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
static Expr * find_duplicate_ors(Expr *qual, bool is_check)
Definition: prepqual.c:405
#define Assert(condition)
Definition: c.h:745
Definition: pg_list.h:50

◆ clamp_row_est()

double clamp_row_est ( double  nrows)

◆ clause_selectivity()

Selectivity clause_selectivity ( PlannerInfo root,
Node clause,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 599 of file clausesel.c.

References arg, generate_unaccent_rules::args, FuncExpr::args, OpExpr::args, bms_is_subset_singleton(), booltestsel(), boolvarsel(), RestrictInfo::clause, RestrictInfo::clause_relids, clause_selectivity(), clauselist_selectivity(), Const::constisnull, Const::constvalue, CurrentOfExpr::cvarno, DatumGetBool, DEBUG4, elog, estimate_expression_value(), find_base_rel(), FuncExpr::funcid, function_selectivity(), get_notclausearg(), FuncExpr::inputcollid, OpExpr::inputcollid, is_andclause(), is_funcclause(), is_notclause(), is_opclause(), is_orclause(), IsA, JOIN_INNER, join_selectivity(), lfirst, RestrictInfo::norm_selec, nulltestsel(), OpExpr::opno, RestrictInfo::orclause, RestrictInfo::outer_selec, RestrictInfo::pseudoconstant, restriction_selectivity(), rowcomparesel(), s1, s2, scalararraysel(), treat_as_join_clause(), RelOptInfo::tuples, RangeQueryClause::var, Var::varlevelsup, and Var::varno.

Referenced by approx_tuple_count(), booltestsel(), clause_selectivity(), clauselist_selectivity_simple(), and consider_new_or_clause().

604 {
605  Selectivity s1 = 0.5; /* default for any unhandled clause type */
606  RestrictInfo *rinfo = NULL;
607  bool cacheable = false;
608 
609  if (clause == NULL) /* can this still happen? */
610  return s1;
611 
612  if (IsA(clause, RestrictInfo))
613  {
614  rinfo = (RestrictInfo *) clause;
615 
616  /*
617  * If the clause is marked pseudoconstant, then it will be used as a
618  * gating qual and should not affect selectivity estimates; hence
619  * return 1.0. The only exception is that a constant FALSE may be
620  * taken as having selectivity 0.0, since it will surely mean no rows
621  * out of the plan. This case is simple enough that we need not
622  * bother caching the result.
623  */
624  if (rinfo->pseudoconstant)
625  {
626  if (!IsA(rinfo->clause, Const))
627  return (Selectivity) 1.0;
628  }
629 
630  /*
631  * If the clause is marked redundant, always return 1.0.
632  */
633  if (rinfo->norm_selec > 1)
634  return (Selectivity) 1.0;
635 
636  /*
637  * If possible, cache the result of the selectivity calculation for
638  * the clause. We can cache if varRelid is zero or the clause
639  * contains only vars of that relid --- otherwise varRelid will affect
640  * the result, so mustn't cache. Outer join quals might be examined
641  * with either their join's actual jointype or JOIN_INNER, so we need
642  * two cache variables to remember both cases. Note: we assume the
643  * result won't change if we are switching the input relations or
644  * considering a unique-ified case, so we only need one cache variable
645  * for all non-JOIN_INNER cases.
646  */
647  if (varRelid == 0 ||
648  bms_is_subset_singleton(rinfo->clause_relids, varRelid))
649  {
650  /* Cacheable --- do we already have the result? */
651  if (jointype == JOIN_INNER)
652  {
653  if (rinfo->norm_selec >= 0)
654  return rinfo->norm_selec;
655  }
656  else
657  {
658  if (rinfo->outer_selec >= 0)
659  return rinfo->outer_selec;
660  }
661  cacheable = true;
662  }
663 
664  /*
665  * Proceed with examination of contained clause. If the clause is an
666  * OR-clause, we want to look at the variant with sub-RestrictInfos,
667  * so that per-subclause selectivities can be cached.
668  */
669  if (rinfo->orclause)
670  clause = (Node *) rinfo->orclause;
671  else
672  clause = (Node *) rinfo->clause;
673  }
674 
675  if (IsA(clause, Var))
676  {
677  Var *var = (Var *) clause;
678 
679  /*
680  * We probably shouldn't ever see an uplevel Var here, but if we do,
681  * return the default selectivity...
682  */
683  if (var->varlevelsup == 0 &&
684  (varRelid == 0 || varRelid == (int) var->varno))
685  {
686  /* Use the restriction selectivity function for a bool Var */
687  s1 = boolvarsel(root, (Node *) var, varRelid);
688  }
689  }
690  else if (IsA(clause, Const))
691  {
692  /* bool constant is pretty easy... */
693  Const *con = (Const *) clause;
694 
695  s1 = con->constisnull ? 0.0 :
696  DatumGetBool(con->constvalue) ? 1.0 : 0.0;
697  }
698  else if (IsA(clause, Param))
699  {
700  /* see if we can replace the Param */
701  Node *subst = estimate_expression_value(root, clause);
702 
703  if (IsA(subst, Const))
704  {
705  /* bool constant is pretty easy... */
706  Const *con = (Const *) subst;
707 
708  s1 = con->constisnull ? 0.0 :
709  DatumGetBool(con->constvalue) ? 1.0 : 0.0;
710  }
711  else
712  {
713  /* XXX any way to do better than default? */
714  }
715  }
716  else if (is_notclause(clause))
717  {
718  /* inverse of the selectivity of the underlying clause */
719  s1 = 1.0 - clause_selectivity(root,
720  (Node *) get_notclausearg((Expr *) clause),
721  varRelid,
722  jointype,
723  sjinfo);
724  }
725  else if (is_andclause(clause))
726  {
727  /* share code with clauselist_selectivity() */
728  s1 = clauselist_selectivity(root,
729  ((BoolExpr *) clause)->args,
730  varRelid,
731  jointype,
732  sjinfo);
733  }
734  else if (is_orclause(clause))
735  {
736  /*
737  * Selectivities for an OR clause are computed as s1+s2 - s1*s2 to
738  * account for the probable overlap of selected tuple sets.
739  *
740  * XXX is this too conservative?
741  */
742  ListCell *arg;
743 
744  s1 = 0.0;
745  foreach(arg, ((BoolExpr *) clause)->args)
746  {
748  (Node *) lfirst(arg),
749  varRelid,
750  jointype,
751  sjinfo);
752 
753  s1 = s1 + s2 - s1 * s2;
754  }
755  }
756  else if (is_opclause(clause) || IsA(clause, DistinctExpr))
757  {
758  OpExpr *opclause = (OpExpr *) clause;
759  Oid opno = opclause->opno;
760 
761  if (treat_as_join_clause(clause, rinfo, varRelid, sjinfo))
762  {
763  /* Estimate selectivity for a join clause. */
764  s1 = join_selectivity(root, opno,
765  opclause->args,
766  opclause->inputcollid,
767  jointype,
768  sjinfo);
769  }
770  else
771  {
772  /* Estimate selectivity for a restriction clause. */
773  s1 = restriction_selectivity(root, opno,
774  opclause->args,
775  opclause->inputcollid,
776  varRelid);
777  }
778 
779  /*
780  * DistinctExpr has the same representation as OpExpr, but the
781  * contained operator is "=" not "<>", so we must negate the result.
782  * This estimation method doesn't give the right behavior for nulls,
783  * but it's better than doing nothing.
784  */
785  if (IsA(clause, DistinctExpr))
786  s1 = 1.0 - s1;
787  }
788  else if (is_funcclause(clause))
789  {
790  FuncExpr *funcclause = (FuncExpr *) clause;
791 
792  /* Try to get an estimate from the support function, if any */
793  s1 = function_selectivity(root,
794  funcclause->funcid,
795  funcclause->args,
796  funcclause->inputcollid,
797  treat_as_join_clause(clause, rinfo,
798  varRelid, sjinfo),
799  varRelid,
800  jointype,
801  sjinfo);
802  }
803  else if (IsA(clause, ScalarArrayOpExpr))
804  {
805  /* Use node specific selectivity calculation function */
806  s1 = scalararraysel(root,
807  (ScalarArrayOpExpr *) clause,
808  treat_as_join_clause(clause, rinfo,
809  varRelid, sjinfo),
810  varRelid,
811  jointype,
812  sjinfo);
813  }
814  else if (IsA(clause, RowCompareExpr))
815  {
816  /* Use node specific selectivity calculation function */
817  s1 = rowcomparesel(root,
818  (RowCompareExpr *) clause,
819  varRelid,
820  jointype,
821  sjinfo);
822  }
823  else if (IsA(clause, NullTest))
824  {
825  /* Use node specific selectivity calculation function */
826  s1 = nulltestsel(root,
827  ((NullTest *) clause)->nulltesttype,
828  (Node *) ((NullTest *) clause)->arg,
829  varRelid,
830  jointype,
831  sjinfo);
832  }
833  else if (IsA(clause, BooleanTest))
834  {
835  /* Use node specific selectivity calculation function */
836  s1 = booltestsel(root,
837  ((BooleanTest *) clause)->booltesttype,
838  (Node *) ((BooleanTest *) clause)->arg,
839  varRelid,
840  jointype,
841  sjinfo);
842  }
843  else if (IsA(clause, CurrentOfExpr))
844  {
845  /* CURRENT OF selects at most one row of its table */
846  CurrentOfExpr *cexpr = (CurrentOfExpr *) clause;
847  RelOptInfo *crel = find_base_rel(root, cexpr->cvarno);
848 
849  if (crel->tuples > 0)
850  s1 = 1.0 / crel->tuples;
851  }
852  else if (IsA(clause, RelabelType))
853  {
854  /* Not sure this case is needed, but it can't hurt */
855  s1 = clause_selectivity(root,
856  (Node *) ((RelabelType *) clause)->arg,
857  varRelid,
858  jointype,
859  sjinfo);
860  }
861  else if (IsA(clause, CoerceToDomain))
862  {
863  /* Not sure this case is needed, but it can't hurt */
864  s1 = clause_selectivity(root,
865  (Node *) ((CoerceToDomain *) clause)->arg,
866  varRelid,
867  jointype,
868  sjinfo);
869  }
870  else
871  {
872  /*
873  * For anything else, see if we can consider it as a boolean variable.
874  * This only works if it's an immutable expression in Vars of a single
875  * relation; but there's no point in us checking that here because
876  * boolvarsel() will do it internally, and return a suitable default
877  * selectivity if not.
878  */
879  s1 = boolvarsel(root, clause, varRelid);
880  }
881 
882  /* Cache the result if possible */
883  if (cacheable)
884  {
885  if (jointype == JOIN_INNER)
886  rinfo->norm_selec = s1;
887  else
888  rinfo->outer_selec = s1;
889  }
890 
891 #ifdef SELECTIVITY_DEBUG
892  elog(DEBUG4, "clause_selectivity: s1 %f", s1);
893 #endif /* SELECTIVITY_DEBUG */
894 
895  return s1;
896 }
Datum constvalue
Definition: primnodes.h:214
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Index varlevelsup
Definition: primnodes.h:191
List * args
Definition: primnodes.h:477
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2320
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:106
Expr * orclause
Definition: pathnodes.h:2016
Selectivity restriction_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, int varRelid)
Definition: plancat.c:1763
double tuples
Definition: pathnodes.h:705
Selectivity rowcomparesel(PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:2170
Relids clause_relids
Definition: pathnodes.h:2000
bool pseudoconstant
Definition: pathnodes.h:1993
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
Definition: nodes.h:529
double Selectivity
Definition: nodes.h:662
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
#define DEBUG4
Definition: elog.h:22
Selectivity scalararraysel(PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1813
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:59
Selectivity norm_selec
Definition: pathnodes.h:2023
static bool treat_as_join_clause(Node *clause, RestrictInfo *rinfo, int varRelid, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:521
static bool bms_is_subset_singleton(const Bitmapset *s, int x)
Definition: clausesel.c:500
Oid funcid
Definition: primnodes.h:469
char * s1
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1695
Selectivity function_selectivity(PlannerInfo *root, Oid funcid, List *args, Oid inputcollid, bool is_join, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1843
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:599
#define DatumGetBool(X)
Definition: postgres.h:393
Selectivity outer_selec
Definition: pathnodes.h:2026
Expr * clause
Definition: pathnodes.h:1985
Index varno
Definition: primnodes.h:184
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:115
char * s2
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:124
Oid inputcollid
Definition: primnodes.h:476
#define lfirst(lc)
Definition: pg_list.h:190
Oid inputcollid
Definition: primnodes.h:521
#define elog(elevel,...)
Definition: elog.h:214
void * arg
Selectivity join_selectivity(PlannerInfo *root, Oid operatorid, List *args, Oid inputcollid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: plancat.c:1802
Oid opno
Definition: primnodes.h:516
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:69
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:374
List * args
Definition: primnodes.h:522
Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1537
Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
Definition: selfuncs.c:1509
bool constisnull
Definition: primnodes.h:215

◆ clauselist_selectivity()

Selectivity clauselist_selectivity ( PlannerInfo root,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo 
)

Definition at line 69 of file clausesel.c.

References clauselist_selectivity_simple(), find_single_rel_for_clauses(), NIL, RTE_RELATION, RelOptInfo::rtekind, s1, statext_clauselist_selectivity(), and RelOptInfo::statlist.

Referenced by add_foreign_grouping_paths(), brincostestimate(), btcostestimate(), calc_joinrel_size_estimate(), clause_selectivity(), compute_semi_anti_join_factors(), cost_agg(), cost_group(), estimate_path_cost_size(), estimate_size(), genericcostestimate(), get_parameterized_baserel_size(), gincostestimate(), postgresGetForeignJoinPaths(), postgresGetForeignRelSize(), and set_baserel_size_estimates().

74 {
75  Selectivity s1 = 1.0;
76  RelOptInfo *rel;
77  Bitmapset *estimatedclauses = NULL;
78 
79  /*
80  * Determine if these clauses reference a single relation. If so, and if
81  * it has extended statistics, try to apply those.
82  */
83  rel = find_single_rel_for_clauses(root, clauses);
84  if (rel && rel->rtekind == RTE_RELATION && rel->statlist != NIL)
85  {
86  /*
87  * Estimate as many clauses as possible using extended statistics.
88  *
89  * 'estimatedclauses' tracks the 0-based list position index of
90  * clauses that we've estimated using extended statistics, and that
91  * should be ignored.
92  */
93  s1 *= statext_clauselist_selectivity(root, clauses, varRelid,
94  jointype, sjinfo, rel,
95  &estimatedclauses);
96  }
97 
98  /*
99  * Apply normal selectivity estimates for the remaining clauses, passing
100  * 'estimatedclauses' so that it skips already estimated ones.
101  */
102  return s1 * clauselist_selectivity_simple(root, clauses, varRelid,
103  jointype, sjinfo,
104  estimatedclauses);
105 }
#define NIL
Definition: pg_list.h:65
List * statlist
Definition: pathnodes.h:703
double Selectivity
Definition: nodes.h:662
Selectivity clauselist_selectivity_simple(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, Bitmapset *estimatedclauses)
Definition: clausesel.c:150
char * s1
Selectivity statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
RTEKind rtekind
Definition: pathnodes.h:695
static RelOptInfo * find_single_rel_for_clauses(PlannerInfo *root, List *clauses)
Definition: clausesel.c:455

◆ clauselist_selectivity_simple()

Selectivity clauselist_selectivity_simple ( PlannerInfo root,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo,
Bitmapset estimatedclauses 
)

Definition at line 150 of file clausesel.c.

References addRangeClause(), generate_unaccent_rules::args, OpExpr::args, bms_is_empty(), bms_is_member(), bms_membership(), BMS_SINGLETON, RestrictInfo::clause, RestrictInfo::clause_relids, clause_selectivity(), DEFAULT_INEQ_SEL, DEFAULT_RANGE_INEQ_SEL, get_oprrest(), RangeQueryClause::have_hibound, RangeQueryClause::have_lobound, RangeQueryClause::hibound, IS_NULL, is_opclause(), is_pseudo_constant_clause(), is_pseudo_constant_clause_relids(), IsA, RestrictInfo::left_relids, lfirst, linitial, list_length(), RangeQueryClause::lobound, lsecond, RangeQueryClause::next, nulltestsel(), NumRelids(), OpExpr::opno, pfree(), RestrictInfo::pseudoconstant, RestrictInfo::right_relids, s1, s2, and RangeQueryClause::var.

Referenced by clauselist_apply_dependencies(), clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

156 {
157  Selectivity s1 = 1.0;
158  RangeQueryClause *rqlist = NULL;
159  ListCell *l;
160  int listidx;
161 
162  /*
163  * If there's exactly one clause (and it was not estimated yet), just go
164  * directly to clause_selectivity(). None of what we might do below is
165  * relevant.
166  */
167  if (list_length(clauses) == 1 && bms_is_empty(estimatedclauses))
168  return clause_selectivity(root, (Node *) linitial(clauses),
169  varRelid, jointype, sjinfo);
170 
171  /*
172  * Anything that doesn't look like a potential rangequery clause gets
173  * multiplied into s1 and forgotten. Anything that does gets inserted into
174  * an rqlist entry.
175  */
176  listidx = -1;
177  foreach(l, clauses)
178  {
179  Node *clause = (Node *) lfirst(l);
180  RestrictInfo *rinfo;
181  Selectivity s2;
182 
183  listidx++;
184 
185  /*
186  * Skip this clause if it's already been estimated by some other
187  * statistics above.
188  */
189  if (bms_is_member(listidx, estimatedclauses))
190  continue;
191 
192  /* Always compute the selectivity using clause_selectivity */
193  s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo);
194 
195  /*
196  * Check for being passed a RestrictInfo.
197  *
198  * If it's a pseudoconstant RestrictInfo, then s2 is either 1.0 or
199  * 0.0; just use that rather than looking for range pairs.
200  */
201  if (IsA(clause, RestrictInfo))
202  {
203  rinfo = (RestrictInfo *) clause;
204  if (rinfo->pseudoconstant)
205  {
206  s1 = s1 * s2;
207  continue;
208  }
209  clause = (Node *) rinfo->clause;
210  }
211  else
212  rinfo = NULL;
213 
214  /*
215  * See if it looks like a restriction clause with a pseudoconstant on
216  * one side. (Anything more complicated than that might not behave in
217  * the simple way we are expecting.) Most of the tests here can be
218  * done more efficiently with rinfo than without.
219  */
220  if (is_opclause(clause) && list_length(((OpExpr *) clause)->args) == 2)
221  {
222  OpExpr *expr = (OpExpr *) clause;
223  bool varonleft = true;
224  bool ok;
225 
226  if (rinfo)
227  {
228  ok = (bms_membership(rinfo->clause_relids) == BMS_SINGLETON) &&
230  rinfo->right_relids) ||
231  (varonleft = false,
233  rinfo->left_relids)));
234  }
235  else
236  {
237  ok = (NumRelids(clause) == 1) &&
239  (varonleft = false,
241  }
242 
243  if (ok)
244  {
245  /*
246  * If it's not a "<"/"<="/">"/">=" operator, just merge the
247  * selectivity in generically. But if it's the right oprrest,
248  * add the clause to rqlist for later processing.
249  */
250  switch (get_oprrest(expr->opno))
251  {
252  case F_SCALARLTSEL:
253  case F_SCALARLESEL:
254  addRangeClause(&rqlist, clause,
255  varonleft, true, s2);
256  break;
257  case F_SCALARGTSEL:
258  case F_SCALARGESEL:
259  addRangeClause(&rqlist, clause,
260  varonleft, false, s2);
261  break;
262  default:
263  /* Just merge the selectivity in generically */
264  s1 = s1 * s2;
265  break;
266  }
267  continue; /* drop to loop bottom */
268  }
269  }
270 
271  /* Not the right form, so treat it generically. */
272  s1 = s1 * s2;
273  }
274 
275  /*
276  * Now scan the rangequery pair list.
277  */
278  while (rqlist != NULL)
279  {
280  RangeQueryClause *rqnext;
281 
282  if (rqlist->have_lobound && rqlist->have_hibound)
283  {
284  /* Successfully matched a pair of range clauses */
285  Selectivity s2;
286 
287  /*
288  * Exact equality to the default value probably means the
289  * selectivity function punted. This is not airtight but should
290  * be good enough.
291  */
292  if (rqlist->hibound == DEFAULT_INEQ_SEL ||
293  rqlist->lobound == DEFAULT_INEQ_SEL)
294  {
296  }
297  else
298  {
299  s2 = rqlist->hibound + rqlist->lobound - 1.0;
300 
301  /* Adjust for double-exclusion of NULLs */
302  s2 += nulltestsel(root, IS_NULL, rqlist->var,
303  varRelid, jointype, sjinfo);
304 
305  /*
306  * A zero or slightly negative s2 should be converted into a
307  * small positive value; we probably are dealing with a very
308  * tight range and got a bogus result due to roundoff errors.
309  * However, if s2 is very negative, then we probably have
310  * default selectivity estimates on one or both sides of the
311  * range that we failed to recognize above for some reason.
312  */
313  if (s2 <= 0.0)
314  {
315  if (s2 < -0.01)
316  {
317  /*
318  * No data available --- use a default estimate that
319  * is small, but not real small.
320  */
322  }
323  else
324  {
325  /*
326  * It's just roundoff error; use a small positive
327  * value
328  */
329  s2 = 1.0e-10;
330  }
331  }
332  }
333  /* Merge in the selectivity of the pair of clauses */
334  s1 *= s2;
335  }
336  else
337  {
338  /* Only found one of a pair, merge it in generically */
339  if (rqlist->have_lobound)
340  s1 *= rqlist->lobound;
341  else
342  s1 *= rqlist->hibound;
343  }
344  /* release storage and advance */
345  rqnext = rqlist->next;
346  pfree(rqlist);
347  rqlist = rqnext;
348  }
349 
350  return s1;
351 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
bool is_pseudo_constant_clause_relids(Node *clause, Relids relids)
Definition: clauses.c:2143
#define DEFAULT_INEQ_SEL
Definition: selfuncs.h:37
Relids clause_relids
Definition: pathnodes.h:2000
bool pseudoconstant
Definition: pathnodes.h:1993
Definition: nodes.h:529
Relids left_relids
Definition: pathnodes.h:2012
double Selectivity
Definition: nodes.h:662
#define lsecond(l)
Definition: pg_list.h:200
void pfree(void *pointer)
Definition: mcxt.c:1057
#define linitial(l)
Definition: pg_list.h:195
char * s1
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1695
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:599
bool is_pseudo_constant_clause(Node *clause)
Definition: clauses.c:2123
struct RangeQueryClause * next
Definition: clausesel.c:35
static void addRangeClause(RangeQueryClause **rqlist, Node *clause, bool varonleft, bool isLTsel, Selectivity s2)
Definition: clausesel.c:359
Selectivity hibound
Definition: clausesel.c:40
Expr * clause
Definition: pathnodes.h:1985
Selectivity lobound
Definition: clausesel.c:39
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
RegProcedure get_oprrest(Oid opno)
Definition: lsyscache.c:1469
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
char * s2
Relids right_relids
Definition: pathnodes.h:2013
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
#define DEFAULT_RANGE_INEQ_SEL
Definition: selfuncs.h:40
Oid opno
Definition: primnodes.h:516
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * args
Definition: primnodes.h:522
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
int NumRelids(Node *clause)
Definition: clauses.c:2165

◆ contain_mutable_functions()

◆ contain_var_clause()

bool contain_var_clause ( Node node)

◆ contain_vars_of_level()

bool contain_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 369 of file var.c.

References contain_vars_of_level_walker(), and query_or_expression_tree_walker().

Referenced by apply_child_basequals(), checkExprIsVarFree(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), convert_EXISTS_to_ANY(), pull_up_simple_values(), pullup_replace_vars_callback(), transformAExprIn(), transformInsertStmt(), transformRangeFunction(), transformRangeTableFunc(), transformSetOperationTree(), and transformValuesClause().

370 {
371  int sublevels_up = levelsup;
372 
375  (void *) &sublevels_up,
376  0);
377 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3412
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:380

◆ contain_volatile_functions()

◆ contain_volatile_functions_not_nextval()

bool contain_volatile_functions_not_nextval ( Node clause)

Definition at line 774 of file clauses.c.

References contain_volatile_functions_not_nextval_walker().

Referenced by BeginCopyFrom().

775 {
777 }
static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context)
Definition: clauses.c:787

◆ count_nonjunk_tlist_entries()

int count_nonjunk_tlist_entries ( List tlist)

Definition at line 208 of file tlist.c.

References lfirst, and TargetEntry::resjunk.

Referenced by get_update_query_targetlist_def(), transformMultiAssignRef(), and transformSubLink().

209 {
210  int len = 0;
211  ListCell *l;
212 
213  foreach(l, tlist)
214  {
215  TargetEntry *tle = (TargetEntry *) lfirst(l);
216 
217  if (!tle->resjunk)
218  len++;
219  }
220  return len;
221 }
bool resjunk
Definition: primnodes.h:1414
#define lfirst(lc)
Definition: pg_list.h:190

◆ estimate_expression_value()

Node* estimate_expression_value ( PlannerInfo root,
Node node 
)

Definition at line 2320 of file clauses.c.

References eval_const_expressions_context::active_fns, eval_const_expressions_context::boundParams, PlannerGlobal::boundParams, eval_const_expressions_context::case_val, eval_const_expressions_context::estimate, eval_const_expressions_mutator(), PlannerInfo::glob, NIL, and eval_const_expressions_context::root.

Referenced by array_unnest_support(), bernoulli_samplescangetsamplesize(), clause_selectivity(), generate_series_int4_support(), generate_series_int8_support(), get_restriction_variable(), gincost_opexpr(), gincost_scalararrayopexpr(), preprocess_limit(), scalararraysel(), system_rows_samplescangetsamplesize(), system_samplescangetsamplesize(), and system_time_samplescangetsamplesize().

2321 {
2323 
2324  context.boundParams = root->glob->boundParams; /* bound Params */
2325  /* we do not need to mark the plan as depending on inlined functions */
2326  context.root = NULL;
2327  context.active_fns = NIL; /* nothing being recursively simplified */
2328  context.case_val = NULL; /* no CASE being examined */
2329  context.estimate = true; /* unsafe transformations OK */
2330  return eval_const_expressions_mutator(node, &context);
2331 }
#define NIL
Definition: pg_list.h:65
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2365
ParamListInfo boundParams
Definition: clauses.c:66
PlannerGlobal * glob
Definition: pathnodes.h:181
ParamListInfo boundParams
Definition: pathnodes.h:109

◆ eval_const_expressions()

Node* eval_const_expressions ( PlannerInfo root,
Node node 
)

Definition at line 2287 of file clauses.c.

References eval_const_expressions_context::active_fns, eval_const_expressions_context::boundParams, PlannerGlobal::boundParams, eval_const_expressions_context::case_val, eval_const_expressions_context::estimate, eval_const_expressions_mutator(), PlannerInfo::glob, NIL, and eval_const_expressions_context::root.

Referenced by apply_child_basequals(), ATExecAttachPartition(), ConstraintImpliedByRelConstraint(), convert_EXISTS_to_ANY(), DoCopy(), expression_planner(), expression_planner_with_deps(), get_proposed_default_constraint(), get_relation_constraints(), preprocess_expression(), preprocess_function_rtes(), process_implied_equality(), RelationBuildPartitionKey(), RelationGetIndexExpressions(), RelationGetIndexPredicate(), and simplify_EXISTS_query().

2288 {
2290 
2291  if (root)
2292  context.boundParams = root->glob->boundParams; /* bound Params */
2293  else
2294  context.boundParams = NULL;
2295  context.root = root; /* for inlined-function dependencies */
2296  context.active_fns = NIL; /* nothing being recursively simplified */
2297  context.case_val = NULL; /* no CASE being examined */
2298  context.estimate = false; /* safe transformations only */
2299  return eval_const_expressions_mutator(node, &context);
2300 }
#define NIL
Definition: pg_list.h:65
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2365
ParamListInfo boundParams
Definition: clauses.c:66
PlannerGlobal * glob
Definition: pathnodes.h:181
ParamListInfo boundParams
Definition: pathnodes.h:109

◆ evaluate_expr()

Expr* evaluate_expr ( Expr expr,
Oid  result_type,
int32  result_typmod,
Oid  result_collation 
)

Definition at line 4756 of file clauses.c.

References CreateExecutorState(), datumCopy(), EState::es_query_cxt, ExecEvalExprSwitchContext(), ExecInitExpr(), fix_opfuncids(), FreeExecutorState(), get_typlenbyval(), GetPerTupleExprContext, makeConst(), MemoryContextSwitchTo(), PG_DETOAST_DATUM_COPY, and PointerGetDatum.

Referenced by eval_const_expressions_mutator(), evaluate_function(), and transformPartitionBoundValue().

4758 {
4759  EState *estate;
4760  ExprState *exprstate;
4761  MemoryContext oldcontext;
4762  Datum const_val;
4763  bool const_is_null;
4764  int16 resultTypLen;
4765  bool resultTypByVal;
4766 
4767  /*
4768  * To use the executor, we need an EState.
4769  */
4770  estate = CreateExecutorState();
4771 
4772  /* We can use the estate's working context to avoid memory leaks. */
4773  oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4774 
4775  /* Make sure any opfuncids are filled in. */
4776  fix_opfuncids((Node *) expr);
4777 
4778  /*
4779  * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
4780  * because it'd result in recursively invoking eval_const_expressions.)
4781  */
4782  exprstate = ExecInitExpr(expr, NULL);
4783 
4784  /*
4785  * And evaluate it.
4786  *
4787  * It is OK to use a default econtext because none of the ExecEvalExpr()
4788  * code used in this situation will use econtext. That might seem
4789  * fortuitous, but it's not so unreasonable --- a constant expression does
4790  * not depend on context, by definition, n'est ce pas?
4791  */
4792  const_val = ExecEvalExprSwitchContext(exprstate,
4793  GetPerTupleExprContext(estate),
4794  &const_is_null);
4795 
4796  /* Get info needed about result datatype */
4797  get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
4798 
4799  /* Get back to outer memory context */
4800  MemoryContextSwitchTo(oldcontext);
4801 
4802  /*
4803  * Must copy result out of sub-context used by expression eval.
4804  *
4805  * Also, if it's varlena, forcibly detoast it. This protects us against
4806  * storing TOAST pointers into plans that might outlive the referenced
4807  * data. (makeConst would handle detoasting anyway, but it's worth a few
4808  * extra lines here so that we can do the copy and detoast in one step.)
4809  */
4810  if (!const_is_null)
4811  {
4812  if (resultTypLen == -1)
4813  const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
4814  else
4815  const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
4816  }
4817 
4818  /* Release all the junk we just created */
4819  FreeExecutorState(estate);
4820 
4821  /*
4822  * Make the constant result node.
4823  */
4824  return (Expr *) makeConst(result_type, result_typmod, result_collation,
4825  resultTypLen,
4826  const_val, const_is_null,
4827  resultTypByVal);
4828 }
signed short int16
Definition: c.h:361
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:305
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242
#define PointerGetDatum(X)
Definition: postgres.h:556
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1636
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:529
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:299
void FreeExecutorState(EState *estate)
Definition: execUtils.c:191
#define GetPerTupleExprContext(estate)
Definition: executor.h:507
MemoryContext es_query_cxt
Definition: execnodes.h:552
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
EState * CreateExecutorState(void)
Definition: execUtils.c:89
uintptr_t Datum
Definition: postgres.h:367
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2139
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123

◆ expand_function_arguments()

List* expand_function_arguments ( List args,
Oid  result_type,
struct HeapTupleData func_tuple 
)

◆ expression_planner()

Expr* expression_planner ( Expr expr)

Definition at line 6178 of file planner.c.

References eval_const_expressions(), and fix_opfuncids().

Referenced by ATExecAddColumn(), ATPrepAlterColumnType(), BeginCopyFrom(), CheckMutability(), ComputePartitionAttrs(), ExecPrepareCheck(), ExecPrepareExpr(), ExecPrepareQual(), load_domaintype_info(), set_baserel_partition_constraint(), slot_fill_defaults(), StoreAttrDefault(), and transformPartitionBoundValue().

6179 {
6180  Node *result;
6181 
6182  /*
6183  * Convert named-argument function calls, insert default arguments and
6184  * simplify constant subexprs
6185  */
6186  result = eval_const_expressions(NULL, (Node *) expr);
6187 
6188  /* Fill in opfuncid values if missing */
6189  fix_opfuncids(result);
6190 
6191  return (Expr *) result;
6192 }
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1636
Definition: nodes.h:529
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2287

◆ expression_planner_with_deps()

Expr* expression_planner_with_deps ( Expr expr,
List **  relationOids,
List **  invalItems 
)

Definition at line 6205 of file planner.c.

References eval_const_expressions(), extract_query_dependencies_walker(), fix_opfuncids(), PlannerInfo::glob, PlannerGlobal::invalItems, MemSet, NIL, PlannerGlobal::relationOids, T_PlannerGlobal, T_PlannerInfo, PlannerGlobal::type, and PlannerInfo::type.

Referenced by GetCachedExpression().

6208 {
6209  Node *result;
6210  PlannerGlobal glob;
6211  PlannerInfo root;
6212 
6213  /* Make up dummy planner state so we can use setrefs machinery */
6214  MemSet(&glob, 0, sizeof(glob));
6215  glob.type = T_PlannerGlobal;
6216  glob.relationOids = NIL;
6217  glob.invalItems = NIL;
6218 
6219  MemSet(&root, 0, sizeof(root));
6220  root.type = T_PlannerInfo;
6221  root.glob = &glob;
6222 
6223  /*
6224  * Convert named-argument function calls, insert default arguments and
6225  * simplify constant subexprs. Collect identities of inlined functions
6226  * and elided domains, too.
6227  */
6228  result = eval_const_expressions(&root, (Node *) expr);
6229 
6230  /* Fill in opfuncid values if missing */
6231  fix_opfuncids(result);
6232 
6233  /*
6234  * Now walk the finished expression to find anything else we ought to
6235  * record as an expression dependency.
6236  */
6237  (void) extract_query_dependencies_walker(result, &root);
6238 
6239  *relationOids = glob.relationOids;
6240  *invalItems = glob.invalItems;
6241 
6242  return (Expr *) result;
6243 }
#define NIL
Definition: pg_list.h:65
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1636
Definition: nodes.h:529
#define MemSet(start, val, len)
Definition: c.h:949
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2287
PlannerGlobal * glob
Definition: pathnodes.h:181
List * invalItems
Definition: pathnodes.h:129
NodeTag type
Definition: pathnodes.h:177
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:2827
NodeTag type
Definition: pathnodes.h:107
List * relationOids
Definition: pathnodes.h:127

◆ extract_query_dependencies()

void extract_query_dependencies ( Node query,
List **  relationOids,
List **  invalItems,
bool hasRowSecurity 
)

Definition at line 2791 of file setrefs.c.

References PlannerGlobal::dependsOnRole, extract_query_dependencies_walker(), PlannerInfo::glob, PlannerGlobal::invalItems, MemSet, NIL, PlannerGlobal::relationOids, T_PlannerGlobal, T_PlannerInfo, PlannerGlobal::type, and PlannerInfo::type.

Referenced by CompleteCachedPlan(), and RevalidateCachedQuery().

2795 {
2796  PlannerGlobal glob;
2797  PlannerInfo root;
2798 
2799  /* Make up dummy planner state so we can use this module's machinery */
2800  MemSet(&glob, 0, sizeof(glob));
2801  glob.type = T_PlannerGlobal;
2802  glob.relationOids = NIL;
2803  glob.invalItems = NIL;
2804  /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
2805  glob.dependsOnRole = false;
2806 
2807  MemSet(&root, 0, sizeof(root));
2808  root.type = T_PlannerInfo;
2809  root.glob = &glob;
2810 
2811  (void) extract_query_dependencies_walker(query, &root);
2812 
2813  *relationOids = glob.relationOids;
2814  *invalItems = glob.invalItems;
2815  *hasRowSecurity = glob.dependsOnRole;
2816 }
#define NIL
Definition: pg_list.h:65
#define MemSet(start, val, len)
Definition: c.h:949
bool dependsOnRole
Definition: pathnodes.h:141
PlannerGlobal * glob
Definition: pathnodes.h:181
List * invalItems
Definition: pathnodes.h:129
NodeTag type
Definition: pathnodes.h:177
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:2827
NodeTag type
Definition: pathnodes.h:107
List * relationOids
Definition: pathnodes.h:127

◆ flatten_join_alias_vars()

Node* flatten_join_alias_vars ( Query query,
Node node 
)

Definition at line 670 of file var.c.

References flatten_join_alias_vars_mutator(), Query::hasSubLinks, flatten_join_alias_vars_context::inserted_sublink, flatten_join_alias_vars_context::possible_sublink, flatten_join_alias_vars_context::query, and flatten_join_alias_vars_context::sublevels_up.

Referenced by finalize_grouping_exprs_walker(), parseCheckAggregates(), preprocess_expression(), pull_up_simple_subquery(), and subquery_planner().

671 {
673 
674  context.query = query;
675  context.sublevels_up = 0;
676  /* flag whether join aliases could possibly contain SubLinks */
677  context.possible_sublink = query->hasSubLinks;
678  /* if hasSubLinks is already true, no need to work hard */
679  context.inserted_sublink = query->hasSubLinks;
680 
681  return flatten_join_alias_vars_mutator(node, &context);
682 }
bool hasSubLinks
Definition: parsenodes.h:128
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:685

◆ get_sortgroupclause_expr()

Node* get_sortgroupclause_expr ( SortGroupClause sgClause,
List targetList 
)

Definition at line 401 of file tlist.c.

References TargetEntry::expr, and get_sortgroupclause_tle().

Referenced by get_sortgrouplist_exprs(), make_pathkeys_for_sortclauses(), transformAggregateCall(), and transformWindowDefinitions().

402 {
403  TargetEntry *tle = get_sortgroupclause_tle(sgClause, targetList);
404 
405  return (Node *) tle->expr;
406 }
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:389
Definition: nodes.h:529
Expr * expr
Definition: primnodes.h:1407

◆ get_sortgroupclause_tle()

◆ get_sortgrouplist_exprs()

List* get_sortgrouplist_exprs ( List sgClauses,
List targetList 
)

Definition at line 414 of file tlist.c.

References get_sortgroupclause_expr(), lappend(), lfirst, and NIL.

Referenced by create_distinct_paths(), estimate_path_cost_size(), get_number_of_groups(), and group_by_has_partkey().

415 {
416  List *result = NIL;
417  ListCell *l;
418 
419  foreach(l, sgClauses)
420  {
421  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
422  Node *sortexpr;
423 
424  sortexpr = get_sortgroupclause_expr(sortcl, targetList);
425  result = lappend(result, sortexpr);
426  }
427  return result;
428 }
#define NIL
Definition: pg_list.h:65
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:401
Definition: nodes.h:529
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:190
Definition: pg_list.h:50

◆ get_sortgroupref_clause()

SortGroupClause* get_sortgroupref_clause ( Index  sortref,
List clauses 
)

Definition at line 444 of file tlist.c.

References elog, ERROR, lfirst, and SortGroupClause::tleSortGroupRef.

Referenced by preprocess_groupclause().

445 {
446  ListCell *l;
447 
448  foreach(l, clauses)
449  {
451 
452  if (cl->tleSortGroupRef == sortref)
453  return cl;
454  }
455 
456  elog(ERROR, "ORDER/GROUP BY expression not found in list");
457  return NULL; /* keep compiler quiet */
458 }
Index tleSortGroupRef
Definition: parsenodes.h:1258
#define ERROR
Definition: elog.h:43
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:214

◆ get_sortgroupref_clause_noerr()

SortGroupClause* get_sortgroupref_clause_noerr ( Index  sortref,
List clauses 
)

Definition at line 465 of file tlist.c.

References lfirst, and SortGroupClause::tleSortGroupRef.

Referenced by find_em_expr_for_input_target(), foreign_grouping_ok(), make_group_input_target(), and make_partial_grouping_target().

466 {
467  ListCell *l;
468 
469  foreach(l, clauses)
470  {
472 
473  if (cl->tleSortGroupRef == sortref)
474  return cl;
475  }
476 
477  return NULL;
478 }
Index tleSortGroupRef
Definition: parsenodes.h:1258
#define lfirst(lc)
Definition: pg_list.h:190

◆ get_sortgroupref_tle()

TargetEntry* get_sortgroupref_tle ( Index  sortref,
List targetList 
)

Definition at line 367 of file tlist.c.

References elog, ERROR, lfirst, and TargetEntry::ressortgroupref.

Referenced by convert_subquery_pathkeys(), deparseSortGroupClause(), foreign_expr_walker(), get_rule_sortgroupclause(), get_sortgroupclause_tle(), make_unique_from_pathkeys(), prepare_sort_from_pathkeys(), and transformDistinctOnClause().

368 {
369  ListCell *l;
370 
371  foreach(l, targetList)
372  {
373  TargetEntry *tle = (TargetEntry *) lfirst(l);
374 
375  if (tle->ressortgroupref == sortref)
376  return tle;
377  }
378 
379  elog(ERROR, "ORDER/GROUP BY expression not found in targetlist");
380  return NULL; /* keep compiler quiet */
381 }
#define ERROR
Definition: elog.h:43
#define lfirst(lc)
Definition: pg_list.h:190
#define elog(elevel,...)
Definition: elog.h:214
Index ressortgroupref
Definition: primnodes.h:1410

◆ is_pseudo_constant_for_index()

bool is_pseudo_constant_for_index ( Node expr,
IndexOptInfo index 
)

Definition at line 3804 of file indxpath.c.

References bms_is_member(), contain_volatile_functions(), pull_varnos(), IndexOptInfo::rel, and RelOptInfo::relid.

3805 {
3806  /* pull_varnos is cheaper than volatility check, so do that first */
3807  if (bms_is_member(index->rel->relid, pull_varnos(expr)))
3808  return false; /* no good, contains Var of table */
3809  if (contain_volatile_functions(expr))
3810  return false; /* no good, volatile comparison value */
3811  return true;
3812 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
RelOptInfo * rel
Definition: pathnodes.h:820
Relids pull_varnos(Node *node)
Definition: var.c:95
Index relid
Definition: pathnodes.h:693
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ locate_var_of_level()

int locate_var_of_level ( Node node,
int  levelsup 
)

Definition at line 437 of file var.c.

References locate_var_of_level_walker(), query_or_expression_tree_walker(), locate_var_of_level_context::sublevels_up, and locate_var_of_level_context::var_location.

Referenced by check_agg_arguments(), checkExprIsVarFree(), and transformSetOperationTree().

438 {
440 
441  context.var_location = -1; /* in case we find nothing */
442  context.sublevels_up = levelsup;
443 
446  (void *) &context,
447  0);
448 
449  return context.var_location;
450 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3412
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:453

◆ negate_clause()

Node* negate_clause ( Node node)

Definition at line 74 of file prepqual.c.

References AND_EXPR, NullTest::arg, BooleanTest::arg, NullTest::argisrow, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, BoolExpr::boolop, BooleanTest::booltesttype, Const::constisnull, Const::constvalue, DatumGetBool, elog, ERROR, get_negator(), OpExpr::inputcollid, ScalarArrayOpExpr::inputcollid, InvalidOid, IS_FALSE, IS_NOT_FALSE, IS_NOT_NULL, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_NULL, IS_TRUE, IS_UNKNOWN, lappend(), lfirst, linitial, OpExpr::location, ScalarArrayOpExpr::location, NullTest::location, BooleanTest::location, make_andclause(), make_notclause(), make_orclause(), makeBoolConst(), makeNode, negate_clause(), NIL, nodeTag, NOT_EXPR, NullTest::nulltesttype, OpExpr::opcollid, OpExpr::opfuncid, ScalarArrayOpExpr::opfuncid, OpExpr::opno, ScalarArrayOpExpr::opno, OpExpr::opresulttype, OpExpr::opretset, OR_EXPR, T_BooleanTest, T_BoolExpr, T_Const, T_NullTest, T_OpExpr, T_ScalarArrayOpExpr, and ScalarArrayOpExpr::useOr.

Referenced by eval_const_expressions_mutator(), match_boolean_partition_clause(), negate_clause(), and simplify_boolean_equality().

75 {
76  if (node == NULL) /* should not happen */
77  elog(ERROR, "can't negate an empty subexpression");
78  switch (nodeTag(node))
79  {
80  case T_Const:
81  {
82  Const *c = (Const *) node;
83 
84  /* NOT NULL is still NULL */
85  if (c->constisnull)
86  return makeBoolConst(false, true);
87  /* otherwise pretty easy */
88  return makeBoolConst(!DatumGetBool(c->constvalue), false);
89  }
90  break;
91  case T_OpExpr:
92  {
93  /*
94  * Negate operator if possible: (NOT (< A B)) => (>= A B)
95  */
96  OpExpr *opexpr = (OpExpr *) node;
97  Oid negator = get_negator(opexpr->opno);
98 
99  if (negator)
100  {
101  OpExpr *newopexpr = makeNode(OpExpr);
102 
103  newopexpr->opno = negator;
104  newopexpr->opfuncid = InvalidOid;
105  newopexpr->opresulttype = opexpr->opresulttype;
106  newopexpr->opretset = opexpr->opretset;
107  newopexpr->opcollid = opexpr->opcollid;
108  newopexpr->inputcollid = opexpr->inputcollid;
109  newopexpr->args = opexpr->args;
110  newopexpr->location = opexpr->location;
111  return (Node *) newopexpr;
112  }
113  }
114  break;
115  case T_ScalarArrayOpExpr:
116  {
117  /*
118  * Negate a ScalarArrayOpExpr if its operator has a negator;
119  * for example x = ANY (list) becomes x <> ALL (list)
120  */
121  ScalarArrayOpExpr *saopexpr = (ScalarArrayOpExpr *) node;
122  Oid negator = get_negator(saopexpr->opno);
123 
124  if (negator)
125  {
127 
128  newopexpr->opno = negator;
129  newopexpr->opfuncid = InvalidOid;
130  newopexpr->useOr = !saopexpr->useOr;
131  newopexpr->inputcollid = saopexpr->inputcollid;
132  newopexpr->args = saopexpr->args;
133  newopexpr->location = saopexpr->location;
134  return (Node *) newopexpr;
135  }
136  }
137  break;
138  case T_BoolExpr:
139  {
140  BoolExpr *expr = (BoolExpr *) node;
141 
142  switch (expr->boolop)
143  {
144  /*--------------------
145  * Apply DeMorgan's Laws:
146  * (NOT (AND A B)) => (OR (NOT A) (NOT B))
147  * (NOT (OR A B)) => (AND (NOT A) (NOT B))
148  * i.e., swap AND for OR and negate each subclause.
149  *
150  * If the input is already AND/OR flat and has no NOT
151  * directly above AND or OR, this transformation preserves
152  * those properties. For example, if no direct child of
153  * the given AND clause is an AND or a NOT-above-OR, then
154  * the recursive calls of negate_clause() can't return any
155  * OR clauses. So we needn't call pull_ors() before
156  * building a new OR clause. Similarly for the OR case.
157  *--------------------
158  */
159  case AND_EXPR:
160  {
161  List *nargs = NIL;
162  ListCell *lc;
163 
164  foreach(lc, expr->args)
165  {
166  nargs = lappend(nargs,
167  negate_clause(lfirst(lc)));
168  }
169  return (Node *) make_orclause(nargs);
170  }
171  break;
172  case OR_EXPR:
173  {
174  List *nargs = NIL;
175  ListCell *lc;
176 
177  foreach(lc, expr->args)
178  {
179  nargs = lappend(nargs,
180  negate_clause(lfirst(lc)));
181  }
182  return (Node *) make_andclause(nargs);
183  }
184  break;
185  case NOT_EXPR:
186 
187  /*
188  * NOT underneath NOT: they cancel. We assume the
189  * input is already simplified, so no need to recurse.
190  */
191  return (Node *) linitial(expr->args);
192  default:
193  elog(ERROR, "unrecognized boolop: %d",
194  (int) expr->boolop);
195  break;
196  }
197  }
198  break;
199  case T_NullTest:
200  {
201  NullTest *expr = (NullTest *) node;
202 
203  /*
204  * In the rowtype case, the two flavors of NullTest are *not*
205  * logical inverses, so we can't simplify. But it does work
206  * for scalar datatypes.
207  */
208  if (!expr->argisrow)
209  {
210  NullTest *newexpr = makeNode(NullTest);
211 
212  newexpr->arg = expr->arg;
213  newexpr->nulltesttype = (expr->nulltesttype == IS_NULL ?
214  IS_NOT_NULL : IS_NULL);
215  newexpr->argisrow = expr->argisrow;
216  newexpr->location = expr->location;
217  return (Node *) newexpr;
218  }
219  }
220  break;
221  case T_BooleanTest:
222  {
223  BooleanTest *expr = (BooleanTest *) node;
224  BooleanTest *newexpr = makeNode(BooleanTest);
225 
226  newexpr->arg = expr->arg;
227  switch (expr->booltesttype)
228  {
229  case IS_TRUE:
230  newexpr->booltesttype = IS_NOT_TRUE;
231  break;
232  case IS_NOT_TRUE:
233  newexpr->booltesttype = IS_TRUE;
234  break;
235  case IS_FALSE:
236  newexpr->booltesttype = IS_NOT_FALSE;
237  break;
238  case IS_NOT_FALSE:
239  newexpr->booltesttype = IS_FALSE;
240  break;
241  case IS_UNKNOWN:
242  newexpr->booltesttype = IS_NOT_UNKNOWN;
243  break;
244  case IS_NOT_UNKNOWN:
245  newexpr->booltesttype = IS_UNKNOWN;
246  break;
247  default:
248  elog(ERROR, "unrecognized booltesttype: %d",
249  (int) expr->booltesttype);
250  break;
251  }
252  newexpr->location = expr->location;
253  return (Node *) newexpr;
254  }
255  break;
256  default:
257  /* else fall through */
258  break;
259  }
260 
261  /*
262  * Otherwise we don't know how to simplify this, so just tack on an
263  * explicit NOT node.
264  */
265  return (Node *) make_notclause((Expr *) node);
266 }
Datum constvalue
Definition: primnodes.h:214
#define NIL
Definition: pg_list.h:65
Expr * make_notclause(Expr *notclause)
Definition: makefuncs.c:667
Node * negate_clause(Node *node)
Definition: prepqual.c:74
Definition: nodes.h:529
unsigned int Oid
Definition: postgres_ext.h:31
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:651
Oid opresulttype
Definition: primnodes.h:518
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
BoolExprType boolop
Definition: primnodes.h:582
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
Expr * arg
Definition: primnodes.h:1219
char * c
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:635
int location
Definition: primnodes.h:523
Expr * arg
Definition: primnodes.h:1242
#define DatumGetBool(X)
Definition: postgres.h:393
Oid opcollid
Definition: primnodes.h:520
Definition: nodes.h:154
List * lappend(List *list, void *datum)
Definition: list.c:321
BoolTestType booltesttype
Definition: primnodes.h:1243
Oid opfuncid
Definition: primnodes.h:517
NullTestType nulltesttype
Definition: primnodes.h:1220
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:577
#define lfirst(lc)
Definition: pg_list.h:190
int location
Definition: primnodes.h:1222
Oid inputcollid
Definition: primnodes.h:521
List * args
Definition: primnodes.h:583
#define nodeTag(nodeptr)
Definition: nodes.h:534
#define elog(elevel,...)
Definition: elog.h:214
bool argisrow
Definition: primnodes.h:1221
Oid opno
Definition: primnodes.h:516
Oid get_negator(Oid opno)
Definition: lsyscache.c:1445
List * args
Definition: primnodes.h:522
Definition: pg_list.h:50
bool constisnull
Definition: primnodes.h:215
bool opretset
Definition: primnodes.h:519

◆ plan_cluster_use_sort()

bool plan_cluster_use_sort ( Oid  tableOid,
Oid  indexOid 
)

Definition at line 6258 of file planner.c.

References AccessShareLock, build_simple_rel(), CMD_SELECT, Query::commandType, cost_qual_eval(), cost_sort(), create_index_path(), create_seqscan_path(), CurrentMemoryContext, enable_indexscan, ForwardScanDirection, get_relation_data_width(), PlannerInfo::glob, RelOptInfo::indexlist, IndexOptInfo::indexoid, IndexOptInfo::indexprs, RangeTblEntry::inFromCl, RangeTblEntry::inh, RangeTblEntry::lateral, lfirst_node, list_make1, maintenance_work_mem, makeNode, NIL, RelOptInfo::pages, PlannerInfo::parse, IndexPath::path, QualCost::per_tuple, PlannerInfo::planner_cxt, PlannerInfo::query_level, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RelOptInfo::reltarget, RelOptInfo::rows, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, setup_simple_rel_arrays(), QualCost::startup, Path::total_cost, PlannerInfo::total_table_pages, RelOptInfo::tuples, PathTarget::width, and PlannerInfo::wt_param_id.

Referenced by copy_table_data().

6259 {
6260  PlannerInfo *root;
6261  Query *query;
6262  PlannerGlobal *glob;
6263  RangeTblEntry *rte;
6264  RelOptInfo *rel;
6265  IndexOptInfo *indexInfo;
6266  QualCost indexExprCost;
6267  Cost comparisonCost;
6268  Path *seqScanPath;
6269  Path seqScanAndSortPath;
6270  IndexPath *indexScanPath;
6271  ListCell *lc;
6272 
6273  /* We can short-circuit the cost comparison if indexscans are disabled */
6274  if (!enable_indexscan)
6275  return true; /* use sort */
6276 
6277  /* Set up mostly-dummy planner state */
6278  query = makeNode(Query);
6279  query->commandType = CMD_SELECT;
6280 
6281  glob = makeNode(PlannerGlobal);
6282 
6283  root = makeNode(PlannerInfo);
6284  root->parse = query;
6285  root->glob = glob;
6286  root->query_level = 1;
6288  root->wt_param_id = -1;
6289 
6290  /* Build a minimal RTE for the rel */
6291  rte = makeNode(RangeTblEntry);
6292  rte->rtekind = RTE_RELATION;
6293  rte->relid = tableOid;
6294  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
6296  rte->lateral = false;
6297  rte->inh = false;
6298  rte->inFromCl = true;
6299  query->rtable = list_make1(rte);
6300 
6301  /* Set up RTE/RelOptInfo arrays */
6303 
6304  /* Build RelOptInfo */
6305  rel = build_simple_rel(root, 1, NULL);
6306 
6307  /* Locate IndexOptInfo for the target index */
6308  indexInfo = NULL;
6309  foreach(lc, rel->indexlist)
6310  {
6311  indexInfo = lfirst_node(IndexOptInfo, lc);
6312  if (indexInfo->indexoid == indexOid)
6313  break;
6314  }
6315 
6316  /*
6317  * It's possible that get_relation_info did not generate an IndexOptInfo
6318  * for the desired index; this could happen if it's not yet reached its
6319  * indcheckxmin usability horizon, or if it's a system index and we're
6320  * ignoring system indexes. In such cases we should tell CLUSTER to not
6321  * trust the index contents but use seqscan-and-sort.
6322  */
6323  if (lc == NULL) /* not in the list? */
6324  return true; /* use sort */
6325 
6326  /*
6327  * Rather than doing all the pushups that would be needed to use
6328  * set_baserel_size_estimates, just do a quick hack for rows and width.
6329  */
6330  rel->rows = rel->tuples;
6331  rel->reltarget->width = get_relation_data_width(tableOid, NULL);
6332 
6333  root->total_table_pages = rel->pages;
6334 
6335  /*
6336  * Determine eval cost of the index expressions, if any. We need to
6337  * charge twice that amount for each tuple comparison that happens during
6338  * the sort, since tuplesort.c will have to re-evaluate the index
6339  * expressions each time. (XXX that's pretty inefficient...)
6340  */
6341  cost_qual_eval(&indexExprCost, indexInfo->indexprs, root);
6342  comparisonCost = 2.0 * (indexExprCost.startup + indexExprCost.per_tuple);
6343 
6344  /* Estimate the cost of seq scan + sort */
6345  seqScanPath = create_seqscan_path(root, rel, NULL, 0);
6346  cost_sort(&seqScanAndSortPath, root, NIL,
6347  seqScanPath->total_cost, rel->tuples, rel->reltarget->width,
6348  comparisonCost, maintenance_work_mem, -1.0);
6349 
6350  /* Estimate the cost of index scan */
6351  indexScanPath = create_index_path(root, indexInfo,
6352  NIL, NIL, NIL, NIL,
6353  ForwardScanDirection, false,
6354  NULL, 1.0, false);
6355 
6356  return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);
6357 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:179
Path path
Definition: pathnodes.h:1206
double tuples
Definition: pathnodes.h:705
#define AccessShareLock
Definition: lockdefs.h:36
Cost startup
Definition: pathnodes.h:45
#define list_make1(x1)
Definition: pg_list.h:227
Cost per_tuple
Definition: pathnodes.h:46
int wt_param_id
Definition: pathnodes.h:353
List * rtable
Definition: parsenodes.h:137
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:4056
#define lfirst_node(type, lc)
Definition: pg_list.h:193
IndexPath * create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, List *indexorderbys, List *indexorderbycols, List *pathkeys, ScanDirection indexscandir, bool indexonly, Relids required_outer, double loop_count, bool partial_path)
Definition: pathnode.c:997
PlannerGlobal * glob
Definition: pathnodes.h:181
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:194
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
double total_table_pages
Definition: pathnodes.h:333
int32 get_relation_data_width(Oid relid, int32 *attr_widths)
Definition: plancat.c:1129
void cost_sort(Path *path, PlannerInfo *root, List *pathkeys, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:1927
List * indexlist
Definition: pathnodes.h:702
double rows
Definition: pathnodes.h:668
int maintenance_work_mem
Definition: globals.c:123
Cost total_cost
Definition: pathnodes.h:1156
CmdType commandType
Definition: parsenodes.h:112
#define makeNode(_type_)
Definition: nodes.h:577
BlockNumber pages
Definition: pathnodes.h:704
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:83
Index query_level
Definition: pathnodes.h:183
RTEKind rtekind
Definition: parsenodes.h:977
MemoryContext planner_cxt
Definition: pathnodes.h:331
struct PathTarget * reltarget
Definition: pathnodes.h:676
bool enable_indexscan
Definition: costsize.c:126
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:929
double Cost
Definition: nodes.h:663
List * indexprs
Definition: pathnodes.h:843

◆ plan_create_index_workers()

int plan_create_index_workers ( Oid  tableOid,
Oid  indexOid 
)

Definition at line 6376 of file planner.c.

References AccessShareLock, build_simple_rel(), CMD_SELECT, Query::commandType, compute_parallel_worker(), CurrentMemoryContext, estimate_rel_size(), PlannerInfo::glob, index_close(), index_open(), RangeTblEntry::inFromCl, RangeTblEntry::inh, is_parallel_safe(), RangeTblEntry::lateral, list_make1, maintenance_work_mem, makeNode, max_parallel_maintenance_workers, Min, NoLock, PlannerInfo::parse, PlannerInfo::planner_cxt, PlannerInfo::query_level, RelationData::rd_rel, RelOptInfo::rel_parallel_workers, RelationGetIndexExpressions(), RelationGetIndexPredicate(), RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, setup_simple_rel_arrays(), table_close(), table_open(), and PlannerInfo::wt_param_id.

Referenced by index_build().

6377 {
6378  PlannerInfo *root;
6379  Query *query;
6380  PlannerGlobal *glob;
6381  RangeTblEntry *rte;
6382  Relation heap;
6383  Relation index;
6384  RelOptInfo *rel;
6385  int parallel_workers;
6386  BlockNumber heap_blocks;
6387  double reltuples;
6388  double allvisfrac;
6389 
6390  /* Return immediately when parallelism disabled */
6392  return 0;
6393 
6394  /* Set up largely-dummy planner state */
6395  query = makeNode(Query);
6396  query->commandType = CMD_SELECT;
6397 
6398  glob = makeNode(PlannerGlobal);
6399 
6400  root = makeNode(PlannerInfo);
6401  root->parse = query;
6402  root->glob = glob;
6403  root->query_level = 1;
6405  root->wt_param_id = -1;
6406 
6407  /*
6408  * Build a minimal RTE.
6409  *
6410  * Mark the RTE with inh = true. This is a kludge to prevent
6411  * get_relation_info() from fetching index info, which is necessary
6412  * because it does not expect that any IndexOptInfo is currently
6413  * undergoing REINDEX.
6414  */
6415  rte = makeNode(RangeTblEntry);
6416  rte->rtekind = RTE_RELATION;
6417  rte->relid = tableOid;
6418  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
6420  rte->lateral = false;
6421  rte->inh = true;
6422  rte->inFromCl = true;
6423  query->rtable = list_make1(rte);
6424 
6425  /* Set up RTE/RelOptInfo arrays */
6427 
6428  /* Build RelOptInfo */
6429  rel = build_simple_rel(root, 1, NULL);
6430 
6431  /* Rels are assumed already locked by the caller */
6432  heap = table_open(tableOid, NoLock);
6433  index = index_open(indexOid, NoLock);
6434 
6435  /*
6436  * Determine if it's safe to proceed.
6437  *
6438  * Currently, parallel workers can't access the leader's temporary tables.
6439  * Furthermore, any index predicate or index expressions must be parallel
6440  * safe.
6441  */
6442  if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
6443  !is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index)) ||
6444  !is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index)))
6445  {
6446  parallel_workers = 0;
6447  goto done;
6448  }
6449 
6450  /*
6451  * If parallel_workers storage parameter is set for the table, accept that
6452  * as the number of parallel worker processes to launch (though still cap
6453  * at max_parallel_maintenance_workers). Note that we deliberately do not
6454  * consider any other factor when parallel_workers is set. (e.g., memory
6455  * use by workers.)
6456  */
6457  if (rel->rel_parallel_workers != -1)
6458  {
6459  parallel_workers = Min(rel->rel_parallel_workers,
6461  goto done;
6462  }
6463 
6464  /*
6465  * Estimate heap relation size ourselves, since rel->pages cannot be
6466  * trusted (heap RTE was marked as inheritance parent)
6467  */
6468  estimate_rel_size(heap, NULL, &heap_blocks, &reltuples, &allvisfrac);
6469 
6470  /*
6471  * Determine number of workers to scan the heap relation using generic
6472  * model
6473  */
6474  parallel_workers = compute_parallel_worker(rel, heap_blocks, -1,
6476 
6477  /*
6478  * Cap workers based on available maintenance_work_mem as needed.
6479  *
6480  * Note that each tuplesort participant receives an even share of the
6481  * total maintenance_work_mem budget. Aim to leave participants
6482  * (including the leader as a participant) with no less than 32MB of
6483  * memory. This leaves cases where maintenance_work_mem is set to 64MB
6484  * immediately past the threshold of being capable of launching a single
6485  * parallel worker to sort.
6486  */
6487  while (parallel_workers > 0 &&
6488  maintenance_work_mem / (parallel_workers + 1) < 32768L)
6489  parallel_workers--;
6490 
6491 done:
6492  index_close(index, NoLock);
6493  table_close(heap, NoLock);
6494 
6495  return parallel_workers;
6496 }
Query * parse
Definition: pathnodes.h:179
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:3827
#define Min(x, y)
Definition: c.h:927
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:529
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:109
List * RelationGetIndexPredicate(Relation relation)
Definition: relcache.c:4861
Definition: type.h:89
#define list_make1(x1)
Definition: pg_list.h:227
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:854
int wt_param_id
Definition: pathnodes.h:353
List * rtable
Definition: parsenodes.h:137
#define NoLock
Definition: lockdefs.h:34
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:4748
PlannerGlobal * glob
Definition: pathnodes.h:181
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:194
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void estimate_rel_size(Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac)
Definition: plancat.c:949
int rel_parallel_workers
Definition: pathnodes.h:711
int max_parallel_maintenance_workers
Definition: globals.c:124
int maintenance_work_mem
Definition: globals.c:123
CmdType commandType
Definition: parsenodes.h:112
#define makeNode(_type_)
Definition: nodes.h:577
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:83
Index query_level
Definition: pathnodes.h:183
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
RTEKind rtekind
Definition: parsenodes.h:977
MemoryContext planner_cxt
Definition: pathnodes.h:331
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132

◆ planner()

struct PlannedStmt* planner ( Query parse,
const char *  query_string,
int  cursorOptions,
struct ParamListInfoData boundParams 
)

◆ predicate_implied_by()

bool predicate_implied_by ( List predicate_list,
List clause_list,
bool  weak 
)

Definition at line 151 of file predtest.c.

References linitial, list_length(), NIL, and predicate_implied_by_recurse().

Referenced by add_predicate_to_index_quals(), build_paths_for_OR(), check_index_predicates(), choose_bitmap_and(), ConstraintImpliedByRelConstraint(), create_bitmap_scan_plan(), create_bitmap_subplan(), create_indexscan_plan(), infer_arbiter_indexes(), and test_predtest().

153 {
154  Node *p,
155  *c;
156 
157  if (predicate_list == NIL)
158  return true; /* no predicate: implication is vacuous */
159  if (clause_list == NIL)
160  return false; /* no restriction: implication must fail */
161 
162  /*
163  * If either input is a single-element list, replace it with its lone
164  * member; this avoids one useless level of AND-recursion. We only need
165  * to worry about this at top level, since eval_const_expressions should
166  * have gotten rid of any trivial ANDs or ORs below that.
167  */
168  if (list_length(predicate_list) == 1)
169  p = (Node *) linitial(predicate_list);
170  else
171  p = (Node *) predicate_list;
172  if (list_length(clause_list) == 1)
173  c = (Node *) linitial(clause_list);
174  else
175  c = (Node *) clause_list;
176 
177  /* And away we go ... */
178  return predicate_implied_by_recurse(c, p, weak);
179 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:529
#define linitial(l)
Definition: pg_list.h:195
char * c
static int list_length(const List *l)
Definition: pg_list.h:169
static bool predicate_implied_by_recurse(Node *clause, Node *predicate, bool weak)
Definition: predtest.c:289

◆ predicate_refuted_by()

bool predicate_refuted_by ( List predicate_list,
List clause_list,
bool  weak 
)

Definition at line 221 of file predtest.c.

References linitial, list_length(), NIL, and predicate_refuted_by_recurse().

Referenced by gen_partprune_steps_internal(), relation_excluded_by_constraints(), and test_predtest().

223 {
224  Node *p,
225  *c;
226 
227  if (predicate_list == NIL)
228  return false; /* no predicate: no refutation is possible */
229  if (clause_list == NIL)
230  return false; /* no restriction: refutation must fail */
231 
232  /*
233  * If either input is a single-element list, replace it with its lone
234  * member; this avoids one useless level of AND-recursion. We only need
235  * to worry about this at top level, since eval_const_expressions should
236  * have gotten rid of any trivial ANDs or ORs below that.
237  */
238  if (list_length(predicate_list) == 1)
239  p = (Node *) linitial(predicate_list);
240  else
241  p = (Node *) predicate_list;
242  if (list_length(clause_list) == 1)
243  c = (Node *) linitial(clause_list);
244  else
245  c = (Node *) clause_list;
246 
247  /* And away we go ... */
248  return predicate_refuted_by_recurse(c, p, weak);
249 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:529
#define linitial(l)
Definition: pg_list.h:195
char * c
static bool predicate_refuted_by_recurse(Node *clause, Node *predicate, bool weak)
Definition: predtest.c:530
static int list_length(const List *l)
Definition: pg_list.h:169

◆ pull_var_clause()

List* pull_var_clause ( Node node,
int  flags 
)

Definition at line 535 of file var.c.

References Assert, pull_var_clause_context::flags, NIL, pull_var_clause_walker(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, and pull_var_clause_context::varlist.

Referenced by AddRelationNewConstraints(), build_base_rel_tlists(), build_remote_returning(), build_tlist_to_deparse(), CreateTrigger(), distribute_qual_to_rels(), estimate_num_groups(), find_placeholders_in_expr(), fix_placeholder_input_needed_levels(), foreign_grouping_ok(), generate_base_implied_equalities_no_const(), make_group_input_target(), make_partial_grouping_target(), make_sort_input_target(), make_window_input_target(), prepare_sort_from_pathkeys(), preprocess_targetlist(), qual_is_pushdown_safe(), and StoreRelCheck().

536 {
537  pull_var_clause_context context;
538 
539  /* Assert that caller has not specified inconsistent flags */
546 
547  context.varlist = NIL;
548  context.flags = flags;
549 
550  pull_var_clause_walker(node, &context);
551  return context.varlist;
552 }
#define NIL
Definition: pg_list.h:65
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:177
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:171
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:173
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:175
#define Assert(condition)
Definition: c.h:745
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:555
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:174
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:172

◆ pull_varattnos()

void pull_varattnos ( Node node,
Index  varno,
Bitmapset **  varattnos 
)

Definition at line 219 of file var.c.

References pull_varattnos_walker(), pull_varattnos_context::varattnos, and pull_varattnos_context::varno.

Referenced by check_index_only(), check_selective_binary_conversion(), ComputePartitionAttrs(), create_foreignscan_plan(), DefineIndex(), deparseReturningList(), fill_extraUpdatedCols(), has_partition_attrs(), postgresGetForeignRelSize(), RelationGetIndexAttrBitmap(), and remove_unused_subquery_outputs().

220 {
221  pull_varattnos_context context;
222 
223  context.varattnos = *varattnos;
224  context.varno = varno;
225 
226  (void) pull_varattnos_walker(node, &context);
227 
228  *varattnos = context.varattnos;
229 }
Bitmapset * varattnos
Definition: var.c:39
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:232

◆ pull_varnos()

Bitmapset* pull_varnos ( Node node)

Definition at line 95 of file var.c.

References pull_varnos_walker(), query_or_expression_tree_walker(), pull_varnos_context::sublevels_up, and pull_varnos_context::varnos.

Referenced by compute_semijoin_info(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), cost_incremental_sort(), distribute_qual_to_rels(), examine_variable(), expand_indexqual_rowcompare(), find_placeholder_info(), get_eclass_for_sort_expr(), is_pseudo_constant_for_index(), IsTidEqualAnyClause(), join_is_removable(), make_outerjoininfo(), make_restrictinfo_internal(), match_rowcompare_to_indexcol(), match_saopclause_to_indexcol(), NumRelids(), pullup_replace_vars_callback(), and reduce_outer_joins_pass2().

96 {
97  pull_varnos_context context;
98 
99  context.varnos = NULL;
100  context.sublevels_up = 0;
101 
102  /*
103  * Must be prepared to start with a Query or a bare expression tree; if
104  * it's a Query, we don't want to increment sublevels_up.
105  */
108  (void *) &context,
109  0);
110 
111  return context.varnos;
112 }
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:140
int sublevels_up
Definition: var.c:34
Relids varnos
Definition: var.c:33
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3412

◆ pull_varnos_of_level()

Bitmapset* pull_varnos_of_level ( Node node,
int  levelsup 
)

Definition at line 120 of file var.c.

References pull_varnos_walker(), query_or_expression_tree_walker(), pull_varnos_context::sublevels_up, and pull_varnos_context::varnos.

Referenced by is_simple_subquery(), and jointree_contains_lateral_outer_refs().

121 {
122  pull_varnos_context context;
123 
124  context.varnos = NULL;
125  context.sublevels_up = levelsup;
126 
127  /*
128  * Must be prepared to start with a Query or a bare expression tree; if
129  * it's a Query, we don't want to increment sublevels_up.
130  */
133  (void *) &context,
134  0);
135 
136  return context.varnos;
137 }
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:140
int sublevels_up
Definition: var.c:34
Relids varnos
Definition: var.c:33
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3412

◆ pull_vars_of_level()

List* pull_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 263 of file var.c.

References NIL, pull_vars_walker(), query_or_expression_tree_walker(), pull_vars_context::sublevels_up, and pull_vars_context::vars.

Referenced by extract_lateral_references().

264 {
265  pull_vars_context context;
266 
267  context.vars = NIL;
268  context.sublevels_up = levelsup;
269 
270  /*
271  * Must be prepared to start with a Query or a bare expression tree; if
272  * it's a Query, we don't want to increment sublevels_up.
273  */
276  (void *) &context,
277  0);
278 
279  return context.vars;
280 }
int sublevels_up
Definition: var.c:46
#define NIL
Definition: pg_list.h:65
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:283
List * vars
Definition: var.c:45
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3412

Variable Documentation

◆ cpu_index_tuple_cost

PGDLLIMPORT double cpu_index_tuple_cost

Definition at line 114 of file costsize.c.

Referenced by genericcostestimate(), and gincostestimate().

◆ cpu_operator_cost

◆ cpu_tuple_cost

◆ effective_cache_size

PGDLLIMPORT int effective_cache_size

Definition at line 119 of file costsize.c.

Referenced by gistBuildCallback(), gistInitBuffering(), and index_pages_fetched().

◆ force_parallel_mode

int force_parallel_mode

Definition at line 70 of file planner.c.

Referenced by HandleParallelMessage(), query_planner(), and standard_planner().

◆ parallel_leader_participation

bool parallel_leader_participation

Definition at line 71 of file planner.c.

Referenced by ExecGather(), ExecGatherMerge(), ExecInitGather(), and get_parallel_divisor().

◆ parallel_setup_cost

PGDLLIMPORT double parallel_setup_cost

Definition at line 117 of file costsize.c.

Referenced by cost_gather(), cost_gather_merge(), and standard_planner().

◆ parallel_tuple_cost

PGDLLIMPORT double parallel_tuple_cost

Definition at line 116 of file costsize.c.

Referenced by cost_gather(), cost_gather_merge(), and standard_planner().

◆ random_page_cost

PGDLLIMPORT double random_page_cost

◆ seq_page_cost