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

Go to the source code of this file.

Data Structures

struct  MVNDistinctItem
 
struct  MVNDistinct
 
struct  MVDependency
 
struct  MVDependencies
 
struct  MCVItem
 
struct  MCVList
 

Macros

#define STATS_MAX_DIMENSIONS   8 /* max number of attributes */
 
#define STATS_NDISTINCT_MAGIC   0xA352BFA4 /* struct identifier */
 
#define STATS_NDISTINCT_TYPE_BASIC   1 /* struct version */
 
#define STATS_DEPS_MAGIC   0xB4549A2C /* marks serialized bytea */
 
#define STATS_DEPS_TYPE_BASIC   1 /* basic dependencies type */
 
#define STATS_MCV_MAGIC   0xE1A651C2 /* marks serialized bytea */
 
#define STATS_MCV_TYPE_BASIC   1 /* basic MCV list type */
 
#define STATS_MCVLIST_MAX_ITEMS   10000
 

Typedefs

typedef struct MVNDistinctItem MVNDistinctItem
 
typedef struct MVNDistinct MVNDistinct
 
typedef struct MVDependency MVDependency
 
typedef struct MVDependencies MVDependencies
 
typedef struct MCVItem MCVItem
 
typedef struct MCVList MCVList
 

Functions

MVNDistinctstatext_ndistinct_load (Oid mvoid)
 
MVDependenciesstatext_dependencies_load (Oid mvoid)
 
MCVListstatext_mcv_load (Oid mvoid)
 
void BuildRelationExtStatistics (Relation onerel, double totalrows, int numrows, HeapTuple *rows, int natts, VacAttrStats **vacattrstats)
 
int ComputeExtStatisticsRows (Relation onerel, int natts, VacAttrStats **stats)
 
bool statext_is_kind_built (HeapTuple htup, char kind)
 
Selectivity dependencies_clauselist_selectivity (PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
 
Selectivity statext_clauselist_selectivity (PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
 
bool has_stats_of_kind (List *stats, char requiredkind)
 
StatisticExtInfochoose_best_statistics (List *stats, Bitmapset *attnums, char requiredkind)
 

Macro Definition Documentation

◆ STATS_DEPS_MAGIC

#define STATS_DEPS_MAGIC   0xB4549A2C /* marks serialized bytea */

Definition at line 42 of file statistics.h.

Referenced by statext_dependencies_build(), and statext_dependencies_deserialize().

◆ STATS_DEPS_TYPE_BASIC

#define STATS_DEPS_TYPE_BASIC   1 /* basic dependencies type */

Definition at line 43 of file statistics.h.

Referenced by statext_dependencies_build(), and statext_dependencies_deserialize().

◆ STATS_MAX_DIMENSIONS

#define STATS_MAX_DIMENSIONS   8 /* max number of attributes */

◆ STATS_MCV_MAGIC

#define STATS_MCV_MAGIC   0xE1A651C2 /* marks serialized bytea */

Definition at line 65 of file statistics.h.

Referenced by statext_mcv_build(), and statext_mcv_deserialize().

◆ STATS_MCV_TYPE_BASIC

#define STATS_MCV_TYPE_BASIC   1 /* basic MCV list type */

Definition at line 66 of file statistics.h.

Referenced by statext_mcv_build(), and statext_mcv_deserialize().

◆ STATS_MCVLIST_MAX_ITEMS

#define STATS_MCVLIST_MAX_ITEMS   10000

Definition at line 69 of file statistics.h.

Referenced by mcv_get_match_bitmap(), and statext_mcv_deserialize().

◆ STATS_NDISTINCT_MAGIC

#define STATS_NDISTINCT_MAGIC   0xA352BFA4 /* struct identifier */

◆ STATS_NDISTINCT_TYPE_BASIC

#define STATS_NDISTINCT_TYPE_BASIC   1 /* struct version */

Typedef Documentation

◆ MCVItem

typedef struct MCVItem MCVItem

◆ MCVList

typedef struct MCVList MCVList

◆ MVDependencies

◆ MVDependency

typedef struct MVDependency MVDependency

◆ MVNDistinct

typedef struct MVNDistinct MVNDistinct

◆ MVNDistinctItem

Function Documentation

◆ BuildRelationExtStatistics()

void BuildRelationExtStatistics ( Relation  onerel,
double  totalrows,
int  numrows,
HeapTuple rows,
int  natts,
VacAttrStats **  vacattrstats 
)

Definition at line 86 of file extended_stats.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, bms_num_members(), StatExtEntry::columns, CurrentMemoryContext, ereport, errcode(), errmsg(), errtable(), fetch_statentries_for_relation(), get_namespace_name(), IsAutoVacuumWorkerProcess(), lfirst, lfirst_int, lookup_var_attr_stats(), MemoryContextDelete(), MemoryContextSwitchTo(), StatExtEntry::name, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, StatExtEntry::schema, stat, statext_compute_stattarget(), statext_dependencies_build(), statext_mcv_build(), statext_ndistinct_build(), statext_store(), StatExtEntry::statOid, STATS_MAX_DIMENSIONS, StatExtEntry::stattarget, table_close(), table_open(), StatExtEntry::types, and WARNING.

Referenced by do_analyze_rel().

89 {
90  Relation pg_stext;
91  ListCell *lc;
92  List *stats;
93  MemoryContext cxt;
94  MemoryContext oldcxt;
95 
97  "BuildRelationExtStatistics",
99  oldcxt = MemoryContextSwitchTo(cxt);
100 
101  pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
102  stats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
103 
104  foreach(lc, stats)
105  {
107  MVNDistinct *ndistinct = NULL;
108  MVDependencies *dependencies = NULL;
109  MCVList *mcv = NULL;
110  VacAttrStats **stats;
111  ListCell *lc2;
112  int stattarget;
113 
114  /*
115  * Check if we can build these stats based on the column analyzed. If
116  * not, report this fact (except in autovacuum) and move on.
117  */
118  stats = lookup_var_attr_stats(onerel, stat->columns,
119  natts, vacattrstats);
120  if (!stats)
121  {
124  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
125  errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
126  stat->schema, stat->name,
127  get_namespace_name(onerel->rd_rel->relnamespace),
128  RelationGetRelationName(onerel)),
129  errtable(onerel)));
130  continue;
131  }
132 
133  /* check allowed number of dimensions */
134  Assert(bms_num_members(stat->columns) >= 2 &&
136 
137  /* compute statistics target for this statistics */
138  stattarget = statext_compute_stattarget(stat->stattarget,
139  bms_num_members(stat->columns),
140  stats);
141 
142  /*
143  * Don't rebuild statistics objects with statistics target set to 0 (we
144  * just leave the existing values around, just like we do for regular
145  * per-column statistics).
146  */
147  if (stattarget == 0)
148  continue;
149 
150  /* compute statistic of each requested type */
151  foreach(lc2, stat->types)
152  {
153  char t = (char) lfirst_int(lc2);
154 
155  if (t == STATS_EXT_NDISTINCT)
156  ndistinct = statext_ndistinct_build(totalrows, numrows, rows,
157  stat->columns, stats);
158  else if (t == STATS_EXT_DEPENDENCIES)
159  dependencies = statext_dependencies_build(numrows, rows,
160  stat->columns, stats);
161  else if (t == STATS_EXT_MCV)
162  mcv = statext_mcv_build(numrows, rows, stat->columns, stats,
163  totalrows, stattarget);
164  }
165 
166  /* store the statistics in the catalog */
167  statext_store(stat->statOid, ndistinct, dependencies, mcv, stats);
168  }
169 
170  table_close(pg_stext, RowExclusiveLock);
171 
172  MemoryContextSwitchTo(oldcxt);
173  MemoryContextDelete(cxt);
174 }
MCVList * statext_mcv_build(int numrows, HeapTuple *rows, Bitmapset *attrs, VacAttrStats **stats, double totalrows, int stattarget)
Definition: mcv.c:183
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static int statext_compute_stattarget(int stattarget, int natts, VacAttrStats **stats)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
static List * fetch_statentries_for_relation(Relation pg_statext, Oid relid)
Form_pg_class rd_rel
Definition: rel.h:83
Bitmapset * columns
#define lfirst_int(lc)
Definition: pg_list.h:191
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:457
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3281
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define stat(a, b)
Definition: win32_port.h:255
MVDependencies * statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs, VacAttrStats **stats)
Definition: dependencies.c:357
MVNDistinct * statext_ndistinct_build(double totalrows, int numrows, HeapTuple *rows, Bitmapset *attrs, VacAttrStats **stats)
Definition: mvdistinct.c:86
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
static void statext_store(Oid relid, MVNDistinct *ndistinct, MVDependencies *dependencies, MCVList *mcv, VacAttrStats **stats)
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
int errtable(Relation rel)
Definition: relcache.c:5167
static VacAttrStats ** lookup_var_attr_stats(Relation rel, Bitmapset *attrs, int nvacatts, VacAttrStats **vacatts)
#define RelationGetRelid(relation)
Definition: rel.h:423

◆ choose_best_statistics()

StatisticExtInfo* choose_best_statistics ( List stats,
Bitmapset attnums,
char  requiredkind 
)

Definition at line 855 of file extended_stats.c.

References bms_free(), bms_intersect(), bms_num_members(), StatisticExtInfo::keys, StatisticExtInfo::kind, lfirst, and STATS_MAX_DIMENSIONS.

Referenced by dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

856 {
857  ListCell *lc;
858  StatisticExtInfo *best_match = NULL;
859  int best_num_matched = 2; /* goal #1: maximize */
860  int best_match_keys = (STATS_MAX_DIMENSIONS + 1); /* goal #2: minimize */
861 
862  foreach(lc, stats)
863  {
864  StatisticExtInfo *info = (StatisticExtInfo *) lfirst(lc);
865  int num_matched;
866  int numkeys;
867  Bitmapset *matched;
868 
869  /* skip statistics that are not of the correct type */
870  if (info->kind != requiredkind)
871  continue;
872 
873  /* determine how many attributes of these stats can be matched to */
874  matched = bms_intersect(attnums, info->keys);
875  num_matched = bms_num_members(matched);
876  bms_free(matched);
877 
878  /*
879  * save the actual number of keys in the stats so that we can choose
880  * the narrowest stats with the most matching keys.
881  */
882  numkeys = bms_num_members(info->keys);
883 
884  /*
885  * Use this object when it increases the number of matched clauses or
886  * when it matches the same number of attributes but these stats have
887  * fewer keys than any previous match.
888  */
889  if (num_matched > best_num_matched ||
890  (num_matched == best_num_matched && numkeys < best_match_keys))
891  {
892  best_match = info;
893  best_num_matched = num_matched;
894  best_match_keys = numkeys;
895  }
896  }
897 
898  return best_match;
899 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * keys
Definition: pathnodes.h:885
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19

◆ ComputeExtStatisticsRows()

int ComputeExtStatisticsRows ( Relation  onerel,
int  natts,
VacAttrStats **  stats 
)

Definition at line 190 of file extended_stats.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, bms_num_members(), StatExtEntry::columns, CurrentMemoryContext, fetch_statentries_for_relation(), lfirst, lookup_var_attr_stats(), MemoryContextDelete(), MemoryContextSwitchTo(), RelationGetRelid, RowExclusiveLock, stat, statext_compute_stattarget(), StatExtEntry::stattarget, table_close(), and table_open().

Referenced by do_analyze_rel().

192 {
193  Relation pg_stext;
194  ListCell *lc;
195  List *lstats;
196  MemoryContext cxt;
197  MemoryContext oldcxt;
198  int result = 0;
199 
201  "ComputeExtStatisticsRows",
203  oldcxt = MemoryContextSwitchTo(cxt);
204 
205  pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock);
206  lstats = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel));
207 
208  foreach(lc, lstats)
209  {
211  int stattarget = stat->stattarget;
212  VacAttrStats **stats;
213  int nattrs = bms_num_members(stat->columns);
214 
215  /*
216  * Check if we can build this statistics object based on the columns
217  * analyzed. If not, ignore it (don't report anything, we'll do that
218  * during the actual build BuildRelationExtStatistics).
219  */
220  stats = lookup_var_attr_stats(onerel, stat->columns,
221  natts, vacattrstats);
222 
223  if (!stats)
224  continue;
225 
226  /*
227  * Compute statistics target, based on what's set for the statistic
228  * object itself, and for its attributes.
229  */
230  stattarget = statext_compute_stattarget(stat->stattarget,
231  nattrs, stats);
232 
233  /* Use the largest value for all statistics objects. */
234  if (stattarget > result)
235  result = stattarget;
236  }
237 
238  table_close(pg_stext, RowExclusiveLock);
239 
240  MemoryContextSwitchTo(oldcxt);
241  MemoryContextDelete(cxt);
242 
243  /* compute sample size based on the statistics target */
244  return (300 * result);
245 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static int statext_compute_stattarget(int stattarget, int natts, VacAttrStats **stats)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static List * fetch_statentries_for_relation(Relation pg_statext, Oid relid)
Bitmapset * columns
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
#define RowExclusiveLock
Definition: lockdefs.h:38
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define stat(a, b)
Definition: win32_port.h:255
#define lfirst(lc)
Definition: pg_list.h:190
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
static VacAttrStats ** lookup_var_attr_stats(Relation rel, Bitmapset *attrs, int nvacatts, VacAttrStats **vacatts)
#define RelationGetRelid(relation)
Definition: rel.h:423

◆ dependencies_clauselist_selectivity()

Selectivity dependencies_clauselist_selectivity ( PlannerInfo root,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo,
RelOptInfo rel,
Bitmapset **  estimatedclauses 
)

Definition at line 941 of file dependencies.c.

References attnum, bms_add_member(), bms_del_member(), bms_is_member(), bms_num_members(), choose_best_statistics(), clause_selectivity(), MVDependency::degree, DependencyGeneratorData::dependencies, dependency_implies_attribute(), dependency_is_compatible_clause(), find_strongest_dependency(), has_stats_of_kind(), InvalidAttrNumber, lfirst, list_length(), palloc(), pfree(), RelOptInfo::relid, s1, s2, stat, statext_dependencies_load(), RelOptInfo::statlist, and StatisticExtInfo::statOid.

Referenced by statext_clauselist_selectivity().

948 {
949  Selectivity s1 = 1.0;
950  ListCell *l;
951  Bitmapset *clauses_attnums = NULL;
953  MVDependencies *dependencies;
954  AttrNumber *list_attnums;
955  int listidx;
956 
957  /* check if there's any stats that might be useful for us. */
958  if (!has_stats_of_kind(rel->statlist, STATS_EXT_DEPENDENCIES))
959  return 1.0;
960 
961  list_attnums = (AttrNumber *) palloc(sizeof(AttrNumber) *
962  list_length(clauses));
963 
964  /*
965  * Pre-process the clauses list to extract the attnums seen in each item.
966  * We need to determine if there's any clauses which will be useful for
967  * dependency selectivity estimations. Along the way we'll record all of
968  * the attnums for each clause in a list which we'll reference later so we
969  * don't need to repeat the same work again. We'll also keep track of all
970  * attnums seen.
971  *
972  * We also skip clauses that we already estimated using different types of
973  * statistics (we treat them as incompatible).
974  */
975  listidx = 0;
976  foreach(l, clauses)
977  {
978  Node *clause = (Node *) lfirst(l);
980 
981  if (!bms_is_member(listidx, *estimatedclauses) &&
982  dependency_is_compatible_clause(clause, rel->relid, &attnum))
983  {
984  list_attnums[listidx] = attnum;
985  clauses_attnums = bms_add_member(clauses_attnums, attnum);
986  }
987  else
988  list_attnums[listidx] = InvalidAttrNumber;
989 
990  listidx++;
991  }
992 
993  /*
994  * If there's not at least two distinct attnums then reject the whole list
995  * of clauses. We must return 1.0 so the calling function's selectivity is
996  * unaffected.
997  */
998  if (bms_num_members(clauses_attnums) < 2)
999  {
1000  pfree(list_attnums);
1001  return 1.0;
1002  }
1003 
1004  /* find the best suited statistics object for these attnums */
1005  stat = choose_best_statistics(rel->statlist, clauses_attnums,
1006  STATS_EXT_DEPENDENCIES);
1007 
1008  /* if no matching stats could be found then we've nothing to do */
1009  if (!stat)
1010  {
1011  pfree(list_attnums);
1012  return 1.0;
1013  }
1014 
1015  /* load the dependency items stored in the statistics object */
1016  dependencies = statext_dependencies_load(stat->statOid);
1017 
1018  /*
1019  * Apply the dependencies recursively, starting with the widest/strongest
1020  * ones, and proceeding to the smaller/weaker ones. At the end of each
1021  * round we factor in the selectivity of clauses on the implied attribute,
1022  * and remove the clauses from the list.
1023  */
1024  while (true)
1025  {
1026  Selectivity s2 = 1.0;
1027  MVDependency *dependency;
1028 
1029  /* the widest/strongest dependency, fully matched by clauses */
1030  dependency = find_strongest_dependency(stat, dependencies,
1031  clauses_attnums);
1032 
1033  /* if no suitable dependency was found, we're done */
1034  if (!dependency)
1035  break;
1036 
1037  /*
1038  * We found an applicable dependency, so find all the clauses on the
1039  * implied attribute - with dependency (a,b => c) we look for clauses
1040  * on 'c'.
1041  */
1042  listidx = -1;
1043  foreach(l, clauses)
1044  {
1045  Node *clause;
1046 
1047  listidx++;
1048 
1049  /*
1050  * Skip incompatible clauses, and ones we've already estimated on.
1051  */
1052  if (list_attnums[listidx] == InvalidAttrNumber)
1053  continue;
1054 
1055  /*
1056  * Technically we could find more than one clause for a given
1057  * attnum. Since these clauses must be equality clauses, we choose
1058  * to only take the selectivity estimate from the final clause in
1059  * the list for this attnum. If the attnum happens to be compared
1060  * to a different Const in another clause then no rows will match
1061  * anyway. If it happens to be compared to the same Const, then
1062  * ignoring the additional clause is just the thing to do.
1063  */
1064  if (dependency_implies_attribute(dependency,
1065  list_attnums[listidx]))
1066  {
1067  clause = (Node *) lfirst(l);
1068 
1069  s2 = clause_selectivity(root, clause, varRelid, jointype,
1070  sjinfo);
1071 
1072  /* mark this one as done, so we don't touch it again. */
1073  *estimatedclauses = bms_add_member(*estimatedclauses, listidx);
1074 
1075  /*
1076  * Mark that we've got and used the dependency on this clause.
1077  * We'll want to ignore this when looking for the next
1078  * strongest dependency above.
1079  */
1080  clauses_attnums = bms_del_member(clauses_attnums,
1081  list_attnums[listidx]);
1082  }
1083  }
1084 
1085  /*
1086  * Now factor in the selectivity for all the "implied" clauses into
1087  * the final one, using this formula:
1088  *
1089  * P(a,b) = P(a) * (f + (1-f) * P(b))
1090  *
1091  * where 'f' is the degree of validity of the dependency.
1092  */
1093  s1 *= (dependency->degree + (1 - dependency->degree) * s2);
1094  }
1095 
1096  pfree(dependencies);
1097  pfree(list_attnums);
1098 
1099  return s1;
1100 }
List * statlist
Definition: pathnodes.h:679
MVDependencies * statext_dependencies_load(Oid mvoid)
Definition: dependencies.c:634
Definition: nodes.h:525
double Selectivity
Definition: nodes.h:658
void pfree(void *pointer)
Definition: mcxt.c:1056
char * s1
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
Selectivity clause_selectivity(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:600
Index relid
Definition: pathnodes.h:669
static bool dependency_implies_attribute(MVDependency *dependency, AttrNumber attnum)
Definition: dependencies.c:621
#define stat(a, b)
Definition: win32_port.h:255
char * s2
StatisticExtInfo * choose_best_statistics(List *stats, Bitmapset *attnums, char requiredkind)
static MVDependency * find_strongest_dependency(StatisticExtInfo *stats, MVDependencies *dependencies, Bitmapset *attnums)
Definition: dependencies.c:865
int16 attnum
Definition: pg_attribute.h:79
#define lfirst(lc)
Definition: pg_list.h:190
double degree
Definition: statistics.h:51
static int list_length(const List *l)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define InvalidAttrNumber
Definition: attnum.h:23
void * palloc(Size size)
Definition: mcxt.c:949
int l
Definition: api.h:16
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
bool has_stats_of_kind(List *stats, char requiredkind)
int16 AttrNumber
Definition: attnum.h:21
static bool dependency_is_compatible_clause(Node *clause, Index relid, AttrNumber *attnum)
Definition: dependencies.c:754

◆ has_stats_of_kind()

bool has_stats_of_kind ( List stats,
char  requiredkind 
)

Definition at line 825 of file extended_stats.c.

References StatisticExtInfo::kind, lfirst, and stat.

Referenced by dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

826 {
827  ListCell *l;
828 
829  foreach(l, stats)
830  {
832 
833  if (stat->kind == requiredkind)
834  return true;
835  }
836 
837  return false;
838 }
#define stat(a, b)
Definition: win32_port.h:255
#define lfirst(lc)
Definition: pg_list.h:190
int l
Definition: api.h:16

◆ statext_clauselist_selectivity()

Selectivity statext_clauselist_selectivity ( PlannerInfo root,
List clauses,
int  varRelid,
JoinType  jointype,
SpecialJoinInfo sjinfo,
RelOptInfo rel,
Bitmapset **  estimatedclauses 
)

Definition at line 1307 of file extended_stats.c.

References dependencies_clauselist_selectivity(), and statext_mcv_clauselist_selectivity().

Referenced by clauselist_selectivity().

1310 {
1311  Selectivity sel;
1312 
1313  /* First, try estimating clauses using a multivariate MCV list. */
1314  sel = statext_mcv_clauselist_selectivity(root, clauses, varRelid, jointype,
1315  sjinfo, rel, estimatedclauses);
1316 
1317  /*
1318  * Then, apply functional dependencies on the remaining clauses by calling
1319  * dependencies_clauselist_selectivity. Pass 'estimatedclauses' so the
1320  * function can properly skip clauses already estimated above.
1321  *
1322  * The reasoning for applying dependencies last is that the more complex
1323  * stats can track more complex correlations between the attributes, and
1324  * so may be considered more reliable.
1325  *
1326  * For example, MCV list can give us an exact selectivity for values in
1327  * two columns, while functional dependencies can only provide information
1328  * about the overall strength of the dependency.
1329  */
1330  sel *= dependencies_clauselist_selectivity(root, clauses, varRelid,
1331  jointype, sjinfo, rel,
1332  estimatedclauses);
1333 
1334  return sel;
1335 }
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
Definition: dependencies.c:941
double Selectivity
Definition: nodes.h:658
static Selectivity statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)

◆ statext_dependencies_load()

MVDependencies* statext_dependencies_load ( Oid  mvoid)

Definition at line 634 of file dependencies.c.

References DatumGetByteaPP, elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1(), statext_dependencies_deserialize(), STATEXTDATASTXOID, and SysCacheGetAttr().

Referenced by dependencies_clauselist_selectivity().

635 {
636  MVDependencies *result;
637  bool isnull;
638  Datum deps;
639  HeapTuple htup;
640 
642  if (!HeapTupleIsValid(htup))
643  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
644 
645  deps = SysCacheGetAttr(STATEXTDATASTXOID, htup,
646  Anum_pg_statistic_ext_data_stxddependencies, &isnull);
647  if (isnull)
648  elog(ERROR,
649  "requested statistic kind \"%c\" is not yet built for statistics object %u",
650  STATS_EXT_DEPENDENCIES, mvoid);
651 
653 
654  ReleaseSysCache(htup);
655 
656  return result;
657 }
#define DatumGetByteaPP(X)
Definition: fmgr.h:285
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:228
MVDependencies * statext_dependencies_deserialize(bytea *data)
Definition: dependencies.c:501

◆ statext_is_kind_built()

bool statext_is_kind_built ( HeapTuple  htup,
char  kind 
)

Definition at line 310 of file extended_stats.c.

References attnum, elog, ERROR, and heap_attisnull().

Referenced by get_relation_statistics(), and UpdateStatisticsForTypeChange().

311 {
313 
314  switch (type)
315  {
316  case STATS_EXT_NDISTINCT:
317  attnum = Anum_pg_statistic_ext_data_stxdndistinct;
318  break;
319 
320  case STATS_EXT_DEPENDENCIES:
321  attnum = Anum_pg_statistic_ext_data_stxddependencies;
322  break;
323 
324  case STATS_EXT_MCV:
325  attnum = Anum_pg_statistic_ext_data_stxdmcv;
326  break;
327 
328  default:
329  elog(ERROR, "unexpected statistics type requested: %d", type);
330  }
331 
332  return !heap_attisnull(htup, attnum, NULL);
333 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
#define ERROR
Definition: elog.h:43
int16 attnum
Definition: pg_attribute.h:79
#define elog(elevel,...)
Definition: elog.h:228
int16 AttrNumber
Definition: attnum.h:21

◆ statext_mcv_load()

MCVList* statext_mcv_load ( Oid  mvoid)

Definition at line 557 of file mcv.c.

References DatumGetByteaP, elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1(), statext_mcv_deserialize(), STATEXTDATASTXOID, and SysCacheGetAttr().

Referenced by mcv_clauselist_selectivity().

558 {
559  MCVList *result;
560  bool isnull;
561  Datum mcvlist;
563 
564  if (!HeapTupleIsValid(htup))
565  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
566 
567  mcvlist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
568  Anum_pg_statistic_ext_data_stxdmcv, &isnull);
569 
570  if (isnull)
571  elog(ERROR,
572  "requested statistic kind \"%c\" is not yet built for statistics object %u",
573  STATS_EXT_DEPENDENCIES, mvoid);
574 
575  result = statext_mcv_deserialize(DatumGetByteaP(mcvlist));
576 
577  ReleaseSysCache(htup);
578 
579  return result;
580 }
MCVList * statext_mcv_deserialize(bytea *data)
Definition: mcv.c:992
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define DatumGetByteaP(X)
Definition: fmgr.h:325
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:228

◆ statext_ndistinct_load()

MVNDistinct* statext_ndistinct_load ( Oid  mvoid)

Definition at line 141 of file mvdistinct.c.

References DatumGetByteaPP, elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1(), statext_ndistinct_deserialize(), STATEXTDATASTXOID, and SysCacheGetAttr().

Referenced by estimate_multivariate_ndistinct().

142 {
143  MVNDistinct *result;
144  bool isnull;
145  Datum ndist;
146  HeapTuple htup;
147 
149  if (!HeapTupleIsValid(htup))
150  elog(ERROR, "cache lookup failed for statistics object %u", mvoid);
151 
152  ndist = SysCacheGetAttr(STATEXTDATASTXOID, htup,
153  Anum_pg_statistic_ext_data_stxdndistinct, &isnull);
154  if (isnull)
155  elog(ERROR,
156  "requested statistic kind \"%c\" is not yet built for statistics object %u",
157  STATS_EXT_NDISTINCT, mvoid);
158 
160 
161  ReleaseSysCache(htup);
162 
163  return result;
164 }
MVNDistinct * statext_ndistinct_deserialize(bytea *data)
Definition: mvdistinct.c:249
#define DatumGetByteaPP(X)
Definition: fmgr.h:285
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:228