44 #include "utils/fmgroids.h"
61 #define WIDTH_THRESHOLD 1024
136 "BuildRelationExtStatistics",
141 if (statslist !=
NIL)
143 const int index[] = {
147 const int64
val[] = {
156 foreach(lc, statslist)
173 natts, vacattrstats);
178 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
179 errmsg(
"statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
204 foreach(lc2,
stat->types)
208 if (t == STATS_EXT_NDISTINCT)
210 else if (t == STATS_EXT_DEPENDENCIES)
212 else if (t == STATS_EXT_MCV)
214 else if (t == STATS_EXT_EXPRESSIONS)
221 elog(
ERROR,
"requested expression stats, but there are no expressions");
236 ndistinct, dependencies, mcv, exprstats, stats);
283 "ComputeExtStatisticsRows",
303 natts, vacattrstats);
316 if (stattarget > result)
326 return (300 * result);
366 for (
i = 0;
i < nattrs;
i++)
369 if (stats[
i]->attstattarget > stattarget)
398 case STATS_EXT_NDISTINCT:
399 attnum = Anum_pg_statistic_ext_data_stxdndistinct;
402 case STATS_EXT_DEPENDENCIES:
403 attnum = Anum_pg_statistic_ext_data_stxddependencies;
407 attnum = Anum_pg_statistic_ext_data_stxdmcv;
410 case STATS_EXT_EXPRESSIONS:
411 attnum = Anum_pg_statistic_ext_data_stxdexpr;
415 elog(
ERROR,
"unexpected statistics type requested: %d",
type);
437 Anum_pg_statistic_ext_stxrelid,
461 for (
i = 0;
i < staForm->stxkeys.dim1;
i++)
464 staForm->stxkeys.values[
i]);
469 Anum_pg_statistic_ext_stxkind);
474 elog(
ERROR,
"stxkind is not a 1-D char array");
478 Assert((enabled[
i] == STATS_EXT_NDISTINCT) ||
479 (enabled[
i] == STATS_EXT_DEPENDENCIES) ||
480 (enabled[
i] == STATS_EXT_MCV) ||
481 (enabled[
i] == STATS_EXT_EXPRESSIONS));
487 Anum_pg_statistic_ext_stxexprs, &isnull);
512 entry->
exprs = exprs;
514 result =
lappend(result, entry);
713 for (
j = 0;
j < nvacatts;
j++)
715 if (
x == vacatts[
j]->tupattnum)
717 stats[
i] = vacatts[
j];
770 bool nulls[Natts_pg_statistic_ext_data];
774 memset(nulls,
true,
sizeof(nulls));
779 nulls[Anum_pg_statistic_ext_data_stxoid - 1] =
false;
782 nulls[Anum_pg_statistic_ext_data_stxdinherit - 1] =
false;
788 if (ndistinct != NULL)
792 nulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] = (
data == NULL);
796 if (dependencies != NULL)
800 nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = (
data == NULL);
807 nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] = (
data == NULL);
810 if (exprs != (
Datum) 0)
812 nulls[Anum_pg_statistic_ext_data_stxdexpr - 1] =
false;
813 values[Anum_pg_statistic_ext_data_stxdexpr - 1] = exprs;
895 b->values[dim],
b->isnull[dim],
906 for (dim = start; dim <= end; dim++)
909 b->values[dim],
b->isnull[dim],
995 int nvalues =
data->numrows * numattrs;
1015 ptr += nvalues *
sizeof(
Datum);
1017 isnull = (
bool *) ptr;
1018 ptr += nvalues *
sizeof(
bool);
1025 for (
i = 0;
i <
data->numrows;
i++)
1028 items[nrows].
isnull = &isnull[nrows * numattrs];
1034 typlen = (
int *)
palloc(
sizeof(
int) *
data->nattnums);
1035 for (
i = 0;
i <
data->nattnums;
i++)
1039 for (
i = 0;
i <
data->numrows;
i++)
1041 bool toowide =
false;
1044 for (
j = 0;
j < numattrs;
j++)
1060 Assert(idx < data->nattnums);
1075 if ((!isnull) && (
attlen == -1))
1087 items[nrows].
isnull[
j] = isnull;
1127 if (
stat->kind == requiredkind)
1148 foreach(lc,
stat->exprs)
1152 if (
equal(stat_expr, expr))
1183 if (expr_idxs != NULL)
1216 int best_num_matched = 2;
1229 if (info->
kind != requiredkind)
1244 for (
i = 0;
i < nclauses;
i++)
1249 if (!clause_attnums[
i] && !clause_exprs[
i])
1279 if (num_matched > best_num_matched ||
1280 (num_matched == best_num_matched && numkeys < best_match_keys))
1283 best_num_matched = num_matched;
1284 best_match_keys = numkeys;
1340 Var *var = (
Var *) clause;
1343 if (var->
varno != relid)
1416 if (
IsA(clause_expr,
Var))
1418 relid, attnums, exprs);
1421 *exprs =
lappend(*exprs, clause_expr);
1483 if (
IsA(clause_expr,
Var))
1485 relid, attnums, exprs);
1488 *exprs =
lappend(*exprs, clause_expr);
1514 foreach(lc, expr->
args)
1522 relid, attnums, exprs))
1537 relid, attnums, exprs);
1548 *exprs =
lappend(*exprs, clause);
1577 RelOptInfo *rel = root->simple_rel_array[relid];
1596 foreach(lc, expr->
args)
1599 relid, attnums, exprs))
1612 if (rinfo->pseudoconstant)
1617 clause_relid != relid)
1622 relid, attnums, exprs))
1760 list_attnums[listidx] = attnums;
1761 list_exprs[listidx] = exprs;
1765 list_attnums[listidx] = NULL;
1766 list_exprs[listidx] =
NIL;
1781 list_attnums, list_exprs,
1798 simple_clauses = NULL;
1818 if (!list_attnums[listidx] && !list_exprs[listidx])
1839 if ((list_attnums[listidx] == NULL &&
1841 (list_exprs[listidx] ==
NIL &&
1855 list_attnums[listidx] = NULL;
1858 list_exprs[listidx] = NULL;
1863 bool *or_matches = NULL;
1882 foreach(l, stat_clauses)
1902 jointype, sjinfo,
false);
1904 overlap_simple_sel = simple_or_sel * simple_sel;
1910 simple_or_sel += simple_sel - overlap_simple_sel;
1919 clause, &or_matches,
1935 clause_sel = simple_sel;
1948 stat_sel += clause_sel - overlap_sel;
1959 sel = sel + stat_sel - sel * stat_sel;
1982 varRelid, jointype, sjinfo,
2014 sjinfo, rel, estimatedclauses, is_or);
2037 jointype, sjinfo, rel,
2080 expr = (
Node *) leftop;
2081 cst = (
Const *) rightop;
2086 expr = (
Node *) rightop;
2087 cst = (
Const *) leftop;
2101 *expronleftp = expronleft;
2121 "Analyze Expression",
2161 exprnulls = (
bool *)
palloc(numrows *
sizeof(
bool));
2164 for (
i = 0;
i < numrows;
i++)
2188 exprvals[tcnt] = (
Datum) 0;
2189 exprnulls[tcnt] =
true;
2199 exprnulls[tcnt] =
false;
2286 thisdata->
expr = expr;
2310 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2311 errmsg(
"relation \"%s\" does not have a composite type",
2314 for (exprno = 0; exprno < nexprs; exprno++)
2321 bool nulls[Natts_pg_statistic];
2337 for (
i = 0;
i < Natts_pg_statistic; ++
i)
2348 i = Anum_pg_statistic_stakind1 - 1;
2353 i = Anum_pg_statistic_staop1 - 1;
2358 i = Anum_pg_statistic_stacoll1 - 1;
2363 i = Anum_pg_statistic_stanumbers1 - 1;
2374 for (n = 0; n < nnum; n++)
2385 i = Anum_pg_statistic_stavalues1 - 1;
2439 elog(
ERROR,
"cache lookup failed for statistics object %u", stxoid);
2442 Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2445 "requested statistics kind \"%c\" is not yet built for statistics object %u",
2446 STATS_EXT_DEPENDENCIES, stxoid);
2523 result->
nulls = (
bool **) ptr;
2524 ptr +=
MAXALIGN(
sizeof(
bool *) * nkeys);
2526 for (
i = 0;
i < nkeys;
i++)
2531 result->
nulls[
i] = (
bool *) ptr;
2532 ptr +=
MAXALIGN(
sizeof(
bool) * numrows);
2553 foreach(lc,
stat->exprs)
2565 for (
i = 0;
i < numrows;
i++)
2593 for (
i = 0;
i < numrows;
i++)
2605 foreach(lc, exprstates)
Datum idx(PG_FUNCTION_ARGS)
AclResult pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
#define DatumGetArrayTypeP(X)
ExpandedArrayHeader * DatumGetExpandedArray(Datum d)
void deconstruct_expanded_array(ExpandedArrayHeader *eah)
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
#define AttributeNumberIsValid(attributeNumber)
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
#define InvalidAttrNumber
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
bool IsAutoVacuumWorkerProcess(void)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
int bms_next_member(const Bitmapset *a, int prevbit)
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
void bms_free(Bitmapset *a)
int bms_num_members(const Bitmapset *a)
bool bms_is_member(int x, const Bitmapset *a)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
BMS_Membership bms_membership(const Bitmapset *a)
bool bms_get_singleton_member(const Bitmapset *a, int *member)
static Datum values[MAXATTR]
#define TextDatumGetCString(d)
#define OidIsValid(objectId)
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Selectivity clause_selectivity_ext(PlannerInfo *root, Node *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, bool use_extended_stats)
Selectivity clauselist_selectivity_ext(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, bool use_extended_stats)
int default_statistics_target
bool std_typanalyze(VacAttrStats *stats)
Datum datumCopy(Datum value, bool typByVal, int typLen)
elog(ERROR, "%s: %s", p2, msg)
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
MVDependencies * statext_dependencies_build(StatsBuildData *data)
Size toast_raw_datum_size(Datum value)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
List * ExecPrepareExprList(List *nodes, EState *estate)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
const TupleTableSlotOps TTSOpsHeapTuple
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
EState * CreateExecutorState(void)
void FreeExecutorState(EState *estate)
#define GetPerTupleExprContext(estate)
#define ResetExprContext(econtext)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
bool has_stats_of_kind(List *stats, char requiredkind)
static AnlExprData * build_expr_data(List *exprs, int stattarget)
int multi_sort_compare_dims(int start, int end, const SortItem *a, const SortItem *b, MultiSortSupport mss)
static bool stat_covers_expressions(StatisticExtInfo *stat, List *exprs, Bitmapset **expr_idxs)
StatisticExtInfo * choose_best_statistics(List *stats, char requiredkind, bool inh, Bitmapset **clause_attnums, List **clause_exprs, int nclauses)
static Datum expr_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull)
static Selectivity statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
int multi_sort_compare_dim(int dim, const SortItem *a, const SortItem *b, MultiSortSupport mss)
int compare_scalars_simple(const void *a, const void *b, void *arg)
int ComputeExtStatisticsRows(Relation onerel, int natts, VacAttrStats **vacattrstats)
static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, VacAttrStats **stats, int stattarget)
struct AnlExprData AnlExprData
SortItem * build_sorted_items(StatsBuildData *data, int *nitems, MultiSortSupport mss, int numattrs, AttrNumber *attnums)
static bool statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
int compare_datums_simple(Datum a, Datum b, SortSupport ssup)
static bool statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs)
static VacAttrStats ** lookup_var_attr_stats(Relation rel, Bitmapset *attrs, List *exprs, int nvacatts, VacAttrStats **vacatts)
static List * fetch_statentries_for_relation(Relation pg_statext, Oid relid)
static void statext_store(Oid statOid, bool inh, MVNDistinct *ndistinct, MVDependencies *dependencies, MCVList *mcv, Datum exprs, VacAttrStats **stats)
bool statext_is_kind_built(HeapTuple htup, char type)
void BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, int numrows, HeapTuple *rows, int natts, VacAttrStats **vacattrstats)
static VacAttrStats * examine_attribute(Node *expr)
Selectivity statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
AttrNumber * build_attnums_array(Bitmapset *attrs, int nexprs, int *numattrs)
static void compute_expr_stats(Relation onerel, double totalrows, AnlExprData *exprdata, int nexprs, HeapTuple *rows, int numrows)
static int stat_find_expression(StatisticExtInfo *stat, Node *expr)
static int statext_compute_stattarget(int stattarget, int nattrs, VacAttrStats **stats)
int multi_sort_compare(const void *a, const void *b, void *arg)
MultiSortSupport multi_sort_init(int ndims)
static VacAttrStats * examine_expression(Node *expr, int stattarget)
HeapTuple statext_expressions_load(Oid stxoid, bool inh, int idx)
static Datum serialize_expr_stats(AnlExprData *exprdata, int nexprs)
void multi_sort_add_dimension(MultiSortSupport mss, int sortdim, Oid oper, Oid collation)
bool examine_opclause_args(List *args, Node **exprp, Const **cstp, bool *expronleftp)
struct StatExtEntry StatExtEntry
MultiSortSupportData * MultiSortSupport
#define OidFunctionCall1(functionId, arg1)
#define DatumGetHeapTupleHeader(X)
#define PG_DETOAST_DATUM(datum)
void systable_endscan(SysScanDesc sysscan)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
static int compare(const void *arg1, const void *arg2)
HeapTuple heap_copytuple(HeapTuple tuple)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#define HeapTupleHeaderGetDatumLength(tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
void list_free(List *list)
RegProcedure get_oprrest(Oid opno)
char * get_namespace_name(Oid nspid)
RegProcedure get_opcode(Oid opno)
Oid get_rel_type_id(Oid relid)
bool get_func_leakproof(Oid funcid)
int16 get_typlen(Oid typid)
Selectivity mcv_clauselist_selectivity(PlannerInfo *root, StatisticExtInfo *stat, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Selectivity *basesel, Selectivity *totalsel)
Selectivity mcv_combine_selectivities(Selectivity simple_sel, Selectivity mcv_sel, Selectivity mcv_basesel, Selectivity mcv_totalsel)
Selectivity mcv_clause_selectivity_or(PlannerInfo *root, StatisticExtInfo *stat, MCVList *mcv, Node *clause, bool **or_matches, Selectivity *basesel, Selectivity *overlap_mcvsel, Selectivity *overlap_basesel, Selectivity *totalsel)
MCVList * statext_mcv_load(Oid mvoid, bool inh)
MCVList * statext_mcv_build(StatsBuildData *data, double totalrows, int stattarget)
bytea * statext_mcv_serialize(MCVList *mcvlist, VacAttrStats **stats)
void MemoryContextReset(MemoryContext context)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
bytea * statext_ndistinct_serialize(MVNDistinct *ndistinct)
MVNDistinct * statext_ndistinct_build(double totalrows, StatsBuildData *data)
Oid exprType(const Node *expr)
int32 exprTypmod(const Node *expr)
Oid exprCollation(const Node *expr)
void fix_opfuncids(Node *node)
static bool is_andclause(const void *clause)
static bool is_orclause(const void *clause)
static bool is_opclause(const void *clause)
static bool is_notclause(const void *clause)
#define IsA(nodeptr, _type_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
#define planner_rt_fetch(rti, root)
static int list_length(const List *l)
#define STATISTIC_NUM_SLOTS
FormData_pg_statistic_ext * Form_pg_statistic_ext
FormData_pg_type * Form_pg_type
void qsort_interruptible(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static Datum Float4GetDatum(float4 X)
static Datum Int16GetDatum(int16 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum Int32GetDatum(int32 X)
#define PROGRESS_ANALYZE_EXT_STATS_COMPUTED
#define PROGRESS_ANALYZE_PHASE
#define PROGRESS_ANALYZE_PHASE_COMPUTE_EXT_STATS
#define PROGRESS_ANALYZE_EXT_STATS_TOTAL
void * stringToNode(const char *str)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
#define RelationGetRelationName(relation)
int errtable(Relation rel)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
#define CLAMP_PROBABILITY(p)
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
struct SortSupportData SortSupportData
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
#define STATS_MAX_DIMENSIONS
void RemoveStatisticsDataById(Oid statsOid, bool inh)
#define BTEqualStrategyNumber
VacAttrStats * vacattrstat
TupleTableSlot * ecxt_scantuple
SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER]
int16 stakind[STATISTIC_NUM_SLOTS]
MemoryContext anl_context
Oid statypid[STATISTIC_NUM_SLOTS]
Oid staop[STATISTIC_NUM_SLOTS]
Oid stacoll[STATISTIC_NUM_SLOTS]
char statypalign[STATISTIC_NUM_SLOTS]
float4 * stanumbers[STATISTIC_NUM_SLOTS]
bool statypbyval[STATISTIC_NUM_SLOTS]
int16 statyplen[STATISTIC_NUM_SLOTS]
int numvalues[STATISTIC_NUM_SLOTS]
Datum * stavalues[STATISTIC_NUM_SLOTS]
int numnumbers[STATISTIC_NUM_SLOTS]
AnalyzeAttrComputeStatsFunc compute_stats
#define FirstLowInvalidHeapAttributeNumber
void ReleaseSysCache(HeapTuple tuple)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
#define SearchSysCacheCopy1(cacheId, key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
#define MAX_STATISTICS_TARGET
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)