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],
991 int nvalues =
data->numrows * numattrs;
1011 ptr += nvalues *
sizeof(
Datum);
1013 isnull = (
bool *) ptr;
1014 ptr += nvalues *
sizeof(bool);
1021 for (
i = 0;
i <
data->numrows;
i++)
1024 items[nrows].isnull = &isnull[nrows * numattrs];
1030 typlen = (
int *)
palloc(
sizeof(
int) *
data->nattnums);
1031 for (
i = 0;
i <
data->nattnums;
i++)
1035 for (
i = 0;
i <
data->numrows;
i++)
1037 bool toowide =
false;
1040 for (
j = 0;
j < numattrs;
j++)
1056 Assert(idx < data->nattnums);
1071 if ((!isnull) && (
attlen == -1))
1083 items[nrows].isnull[
j] = isnull;
1123 if (
stat->kind == requiredkind)
1144 foreach(lc,
stat->exprs)
1148 if (
equal(stat_expr, expr))
1179 if (expr_idxs != NULL)
1212 int best_num_matched = 2;
1225 if (info->
kind != requiredkind)
1240 for (
i = 0;
i < nclauses;
i++)
1245 if (!clause_attnums[
i] && !clause_exprs[
i])
1275 if (num_matched > best_num_matched ||
1276 (num_matched == best_num_matched && numkeys < best_match_keys))
1279 best_num_matched = num_matched;
1280 best_match_keys = numkeys;
1336 Var *var = (
Var *) clause;
1339 if (var->
varno != relid)
1407 if (rte->securityQuals !=
NIL &&
1412 if (
IsA(clause_expr,
Var))
1414 relid, attnums, exprs);
1417 *exprs =
lappend(*exprs, clause_expr);
1474 if (rte->securityQuals !=
NIL &&
1479 if (
IsA(clause_expr,
Var))
1481 relid, attnums, exprs);
1484 *exprs =
lappend(*exprs, clause_expr);
1510 foreach(lc, expr->
args)
1518 relid, attnums, exprs))
1533 relid, attnums, exprs);
1544 *exprs =
lappend(*exprs, clause);
1592 foreach(lc, expr->
args)
1595 relid, attnums, exprs))
1608 if (rinfo->pseudoconstant)
1613 clause_relid != relid)
1618 relid, attnums, exprs))
1756 list_attnums[listidx] = attnums;
1757 list_exprs[listidx] = exprs;
1761 list_attnums[listidx] = NULL;
1762 list_exprs[listidx] =
NIL;
1777 list_attnums, list_exprs,
1794 simple_clauses = NULL;
1814 if (!list_attnums[listidx] && !list_exprs[listidx])
1835 if ((list_attnums[listidx] == NULL &&
1837 (list_exprs[listidx] ==
NIL &&
1851 list_attnums[listidx] = NULL;
1854 list_exprs[listidx] = NULL;
1859 bool *or_matches = NULL;
1878 foreach(l, stat_clauses)
1898 jointype, sjinfo,
false);
1900 overlap_simple_sel = simple_or_sel * simple_sel;
1906 simple_or_sel += simple_sel - overlap_simple_sel;
1915 clause, &or_matches,
1931 clause_sel = simple_sel;
1944 stat_sel += clause_sel - overlap_sel;
1955 sel = sel + stat_sel - sel * stat_sel;
1978 varRelid, jointype, sjinfo,
2010 sjinfo, rel, estimatedclauses, is_or);
2033 jointype, sjinfo, rel,
2076 expr = (
Node *) leftop;
2077 cst = (
Const *) rightop;
2082 expr = (
Node *) rightop;
2083 cst = (
Const *) leftop;
2097 *expronleftp = expronleft;
2116 "Analyze Expression",
2156 exprnulls = (
bool *)
palloc(numrows *
sizeof(
bool));
2159 for (
i = 0;
i < numrows;
i++)
2183 exprvals[tcnt] = (
Datum) 0;
2184 exprnulls[tcnt] =
true;
2194 exprnulls[tcnt] =
false;
2281 thisdata->
expr = expr;
2305 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2306 errmsg(
"relation \"%s\" does not have a composite type",
2309 for (exprno = 0; exprno < nexprs; exprno++)
2316 bool nulls[Natts_pg_statistic];
2332 for (
i = 0;
i < Natts_pg_statistic; ++
i)
2343 i = Anum_pg_statistic_stakind1 - 1;
2348 i = Anum_pg_statistic_staop1 - 1;
2353 i = Anum_pg_statistic_stacoll1 - 1;
2358 i = Anum_pg_statistic_stanumbers1 - 1;
2369 for (n = 0; n < nnum; n++)
2380 i = Anum_pg_statistic_stavalues1 - 1;
2434 elog(
ERROR,
"cache lookup failed for statistics object %u", stxoid);
2437 Anum_pg_statistic_ext_data_stxdexpr, &isnull);
2440 "requested statistics kind \"%c\" is not yet built for statistics object %u",
2441 STATS_EXT_EXPRESSIONS, stxoid);
2518 result->
nulls = (
bool **) ptr;
2519 ptr +=
MAXALIGN(
sizeof(
bool *) * nkeys);
2521 for (
i = 0;
i < nkeys;
i++)
2526 result->
nulls[
i] = (
bool *) ptr;
2527 ptr +=
MAXALIGN(
sizeof(
bool) * numrows);
2548 foreach(lc,
stat->exprs)
2560 for (
i = 0;
i < numrows;
i++)
2588 for (
i = 0;
i < numrows;
i++)
2600 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)
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 Assert(condition)
#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)
AttrNumber * build_attnums_array(Bitmapset *attrs, int nexprs, int *numattrs)
struct AnlExprData AnlExprData
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 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)
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)
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)
#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)