42#include "utils/fmgroids.h"
58#define WIDTH_THRESHOLD 1024
93 int nexprs,
HeapTuple *rows,
int numrows);
132 "BuildRelationExtStatistics",
137 if (statslist !=
NIL)
139 const int index[] = {
152 foreach(lc, statslist)
169 natts, vacattrstats);
174 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
175 errmsg(
"statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"",
200 foreach(lc2,
stat->types)
204 if (t == STATS_EXT_NDISTINCT)
206 else if (t == STATS_EXT_DEPENDENCIES)
208 else if (t == STATS_EXT_MCV)
210 else if (t == STATS_EXT_EXPRESSIONS)
217 elog(
ERROR,
"requested expression stats, but there are no expressions");
230 ndistinct, dependencies, mcv, exprstats, stats);
277 "ComputeExtStatisticsRows",
297 natts, vacattrstats);
310 if (stattarget > result)
320 return (300 * result);
360 for (
i = 0;
i < nattrs;
i++)
363 if (stats[
i]->attstattarget > stattarget)
392 case STATS_EXT_NDISTINCT:
393 attnum = Anum_pg_statistic_ext_data_stxdndistinct;
396 case STATS_EXT_DEPENDENCIES:
397 attnum = Anum_pg_statistic_ext_data_stxddependencies;
401 attnum = Anum_pg_statistic_ext_data_stxdmcv;
404 case STATS_EXT_EXPRESSIONS:
405 attnum = Anum_pg_statistic_ext_data_stxdexpr;
409 elog(
ERROR,
"unexpected statistics type requested: %d",
type);
431 Anum_pg_statistic_ext_stxrelid,
454 for (
i = 0;
i < staForm->stxkeys.dim1;
i++)
457 staForm->stxkeys.values[
i]);
460 datum =
SysCacheGetAttr(STATEXTOID, htup, Anum_pg_statistic_ext_stxstattarget, &isnull);
465 Anum_pg_statistic_ext_stxkind);
470 elog(
ERROR,
"stxkind is not a 1-D char array");
474 Assert((enabled[
i] == STATS_EXT_NDISTINCT) ||
475 (enabled[
i] == STATS_EXT_DEPENDENCIES) ||
476 (enabled[
i] == STATS_EXT_MCV) ||
477 (enabled[
i] == STATS_EXT_EXPRESSIONS));
483 Anum_pg_statistic_ext_stxexprs, &isnull);
508 entry->
exprs = exprs;
510 result =
lappend(result, entry);
709 for (
j = 0;
j < nvacatts;
j++)
711 if (
x == vacatts[
j]->tupattnum)
713 stats[
i] = vacatts[
j];
766 bool nulls[Natts_pg_statistic_ext_data];
770 memset(nulls,
true,
sizeof(nulls));
775 nulls[Anum_pg_statistic_ext_data_stxoid - 1] =
false;
778 nulls[Anum_pg_statistic_ext_data_stxdinherit - 1] =
false;
784 if (ndistinct != NULL)
788 nulls[Anum_pg_statistic_ext_data_stxdndistinct - 1] = (
data == NULL);
792 if (dependencies != NULL)
796 nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = (
data == NULL);
803 nulls[Anum_pg_statistic_ext_data_stxdmcv - 1] = (
data == NULL);
806 if (exprs != (
Datum) 0)
808 nulls[Anum_pg_statistic_ext_data_stxdexpr - 1] =
false;
809 values[Anum_pg_statistic_ext_data_stxdexpr - 1] = exprs;
891 b->values[dim],
b->isnull[dim],
902 for (dim =
start; dim <= end; dim++)
905 b->values[dim],
b->isnull[dim],
990 int nvalues =
data->numrows * numattrs;
1000 nvalues * (
sizeof(
Datum) +
sizeof(
bool));
1012 ptr += nvalues *
sizeof(
Datum);
1014 isnull = (
bool *) ptr;
1015 ptr += nvalues *
sizeof(bool);
1022 for (
i = 0;
i <
data->numrows;
i++)
1025 items[nrows].isnull = &isnull[nrows * numattrs];
1031 typlen = (
int *)
palloc(
sizeof(
int) *
data->nattnums);
1032 for (
i = 0;
i <
data->nattnums;
i++)
1036 for (
i = 0;
i <
data->numrows;
i++)
1038 bool toowide =
false;
1041 for (
j = 0;
j < numattrs;
j++)
1057 Assert(idx < data->nattnums);
1072 if ((!isnull) && (
attlen == -1))
1084 items[nrows].isnull[
j] = isnull;
1124 if (
stat->kind == requiredkind)
1145 foreach(lc,
stat->exprs)
1149 if (
equal(stat_expr, expr))
1180 if (expr_idxs != NULL)
1213 int best_num_matched = 2;
1226 if (info->
kind != requiredkind)
1241 for (
i = 0;
i < nclauses;
i++)
1246 if (!clause_attnums[
i] && !clause_exprs[
i])
1276 if (num_matched > best_num_matched ||
1277 (num_matched == best_num_matched && numkeys < best_match_keys))
1280 best_num_matched = num_matched;
1281 best_match_keys = numkeys;
1331 List **exprs,
bool *leakproof)
1340 Var *var = (
Var *) clause;
1343 if (var->
varno != relid)
1405 if (
IsA(clause_expr,
Var))
1411 *exprs =
lappend(*exprs, clause_expr);
1462 if (
IsA(clause_expr,
Var))
1468 *exprs =
lappend(*exprs, clause_expr);
1494 foreach(lc, expr->
args)
1502 relid, attnums, exprs,
1531 *exprs =
lappend(*exprs, clause);
1577 foreach(lc, expr->
args)
1580 relid, attnums, exprs))
1593 if (rinfo->pseudoconstant)
1598 clause_relid != relid)
1607 relid, attnums, exprs,
1737 list_attnums[listidx] = attnums;
1738 list_exprs[listidx] = exprs;
1742 list_attnums[listidx] = NULL;
1743 list_exprs[listidx] =
NIL;
1758 list_attnums, list_exprs,
1775 simple_clauses = NULL;
1795 if (!list_attnums[listidx] && !list_exprs[listidx])
1816 if ((list_attnums[listidx] == NULL &&
1818 (list_exprs[listidx] ==
NIL &&
1832 list_attnums[listidx] = NULL;
1835 list_exprs[listidx] = NULL;
1840 bool *or_matches = NULL;
1859 foreach(l, stat_clauses)
1879 jointype, sjinfo,
false);
1881 overlap_simple_sel = simple_or_sel * simple_sel;
1887 simple_or_sel += simple_sel - overlap_simple_sel;
1896 clause, &or_matches,
1912 clause_sel = simple_sel;
1925 stat_sel += clause_sel - overlap_sel;
1936 sel = sel + stat_sel - sel * stat_sel;
1959 varRelid, jointype, sjinfo,
1991 sjinfo, rel, estimatedclauses, is_or);
2014 jointype, sjinfo, rel,
2057 expr = (
Node *) leftop;
2058 cst = (
Const *) rightop;
2063 expr = (
Node *) rightop;
2064 cst = (
Const *) leftop;
2078 *expronleftp = expronleft;
2097 "Analyze Expression",
2137 exprnulls = (
bool *)
palloc(numrows *
sizeof(
bool));
2140 for (
i = 0;
i < numrows;
i++)
2164 exprvals[tcnt] = (
Datum) 0;
2165 exprnulls[tcnt] =
true;
2175 exprnulls[tcnt] =
false;
2262 thisdata->
expr = expr;
2286 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2287 errmsg(
"relation \"%s\" does not have a composite type",
2290 for (exprno = 0; exprno < nexprs; exprno++)
2297 bool nulls[Natts_pg_statistic];
2313 for (
i = 0;
i < Natts_pg_statistic; ++
i)
2324 i = Anum_pg_statistic_stakind1 - 1;
2329 i = Anum_pg_statistic_staop1 - 1;
2334 i = Anum_pg_statistic_stacoll1 - 1;
2339 i = Anum_pg_statistic_stanumbers1 - 1;
2350 for (n = 0; n < nnum; n++)
2361 i = Anum_pg_statistic_stavalues1 - 1;
2415 elog(
ERROR,
"cache lookup failed for statistics object %u", stxoid);
2418 Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2421 "requested statistics kind \"%c\" is not yet built for statistics object %u",
2422 STATS_EXT_EXPRESSIONS, stxoid);
2499 result->
nulls = (
bool **) ptr;
2500 ptr +=
MAXALIGN(
sizeof(
bool *) * nkeys);
2502 for (
i = 0;
i < nkeys;
i++)
2507 result->
nulls[
i] = (
bool *) ptr;
2508 ptr +=
MAXALIGN(
sizeof(
bool) * numrows);
2529 foreach(lc,
stat->exprs)
2541 for (
i = 0;
i < numrows;
i++)
2569 for (
i = 0;
i < numrows;
i++)
2581 foreach(lc, exprstates)
Datum idx(PG_FUNCTION_ARGS)
#define DatumGetArrayTypeP(X)
void deconstruct_expanded_array(ExpandedArrayHeader *eah)
ExpandedArrayHeader * DatumGetExpandedArray(Datum d)
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
#define AttributeNumberIsValid(attributeNumber)
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
#define InvalidAttrNumber
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
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)
MVDependencies * statext_dependencies_build(StatsBuildData *data)
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
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)
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
const TupleTableSlotOps TTSOpsHeapTuple
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
void FreeExecutorState(EState *estate)
EState * CreateExecutorState(void)
#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)
static VacAttrStats ** lookup_var_attr_stats(Bitmapset *attrs, List *exprs, int nvacatts, VacAttrStats **vacatts)
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)
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)
static VacAttrStats * examine_attribute(Node *expr)
StatisticExtInfo * choose_best_statistics(List *stats, char requiredkind, bool inh, Bitmapset **clause_attnums, List **clause_exprs, int nclauses)
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)
static bool statext_is_compatible_clause_internal(PlannerInfo *root, Node *clause, Index relid, Bitmapset **attnums, List **exprs, bool *leakproof)
AttrNumber * build_attnums_array(Bitmapset *attrs, int nexprs, int *numattrs)
struct AnlExprData AnlExprData
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 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)
static VacAttrStats * examine_expression(Node *expr, int stattarget)
void BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, int numrows, HeapTuple *rows, int natts, VacAttrStats **vacattrstats)
Selectivity statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
SortItem * build_sorted_items(StatsBuildData *data, int *nitems, MultiSortSupport mss, int numattrs, AttrNumber *attnums)
static int stat_find_expression(StatisticExtInfo *stat, Node *expr)
static int statext_compute_stattarget(int stattarget, int nattrs, VacAttrStats **stats)
static void compute_expr_stats(Relation onerel, AnlExprData *exprdata, int nexprs, HeapTuple *rows, int numrows)
int multi_sort_compare(const void *a, const void *b, void *arg)
MultiSortSupport multi_sort_init(int ndims)
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)
Assert(PointerIsAligned(start, uint64))
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)
static uint32 HeapTupleHeaderGetDatumLength(const HeapTupleHeaderData *tup)
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
static void ItemPointerSetInvalid(ItemPointerData *pointer)
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
void list_free(List *list)
RegProcedure get_oprrest(Oid opno)
RegProcedure get_opcode(Oid opno)
Oid get_rel_type_id(Oid relid)
bool get_func_leakproof(Oid funcid)
int16 get_typlen(Oid typid)
char * get_namespace_name(Oid nspid)
Selectivity mcv_clauselist_selectivity(PlannerInfo *root, StatisticExtInfo *stat, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Selectivity *basesel, Selectivity *totalsel)
MCVList * statext_mcv_load(Oid mvoid, bool inh)
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_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
#define AmAutoVacuumWorkerProcess()
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)
static int16 DatumGetInt16(Datum 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)
bool all_rows_selectable(PlannerInfo *root, Index varno, Bitmapset *varattnos)
#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)