31 #include "utils/fmgroids.h"
32 #include "utils/fmgrprotos.h"
40 #define SizeOfHeader (3 * sizeof(uint32))
43 #define SizeOfItem(natts) \
44 (sizeof(double) + sizeof(AttrNumber) * (1 + (natts)))
47 #define MinSizeOfItem SizeOfItem(2)
50 #define MinSizeOfItems(ndeps) \
51 (SizeOfHeader + (ndeps) * MinSizeOfItem)
151 state->ndependencies++;
179 Assert((n >= k) && (k > 0));
185 state->ndependencies = 0;
233 int n_violations = 0;
236 int n_supporting_rows = 0;
249 for (
i = 0;
i < k;
i++)
250 attnums_dep[
i] =
data->attnums[dependency[
i]];
266 for (
i = 0;
i < k;
i++)
273 elog(
ERROR,
"cache lookup failed for ordering operator for type %u",
314 if (n_violations == 0)
315 n_supporting_rows += group_size;
330 return (n_supporting_rows * 1.0 /
data->numrows);
363 "dependency_degree cxt",
372 for (k = 2; k <=
data->nattnums; k++)
407 for (
i = 0;
i < k;
i++)
411 if (dependencies == NULL)
418 dependencies->
ndeps = 0;
421 dependencies->
ndeps++;
426 dependencies->
deps[dependencies->
ndeps - 1] = d;
457 for (
i = 0;
i < dependencies->
ndeps;
i++)
468 memcpy(tmp, &dependencies->
type,
sizeof(
uint32));
474 for (
i = 0;
i < dependencies->
ndeps;
i++)
478 memcpy(tmp, &d->
degree,
sizeof(
double));
479 tmp +=
sizeof(double);
504 Size min_expected_size;
512 elog(
ERROR,
"invalid MVDependencies size %zu (expected at least %zu)",
524 memcpy(&dependencies->
type, tmp,
sizeof(
uint32));
530 elog(
ERROR,
"invalid dependency magic %d (expected %d)",
534 elog(
ERROR,
"invalid dependency type %d (expected %d)",
537 if (dependencies->
ndeps == 0)
538 elog(
ERROR,
"invalid zero-length item array in MVDependencies");
544 elog(
ERROR,
"invalid dependencies size %zu (expected at least %zu)",
551 for (
i = 0;
i < dependencies->
ndeps;
i++)
558 memcpy(°ree, tmp,
sizeof(
double));
559 tmp +=
sizeof(double);
579 dependencies->
deps[
i] = d;
632 elog(
ERROR,
"cache lookup failed for statistics object %u", mvoid);
635 Anum_pg_statistic_ext_data_stxddependencies, &isnull);
638 "requested statistics kind \"%c\" is not yet built for statistics object %u",
639 STATS_EXT_DEPENDENCIES, mvoid);
662 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
663 errmsg(
"cannot accept a value of type %s",
"pg_dependencies")));
683 for (
i = 0;
i < dependencies->
ndeps;
i++)
715 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
716 errmsg(
"cannot accept a value of type %s",
"pg_dependencies")));
753 if (rinfo->pseudoconstant)
844 foreach(lc, bool_expr->
args)
853 relid, &clause_attnum))
860 if (*
attnum != clause_attnum)
881 clause_expr = (
Node *) clause;
892 if (!
IsA(clause_expr,
Var))
896 var = (
Var *) clause_expr;
899 if (var->
varno != relid)
946 for (
i = 0;
i < ndependencies;
i++)
948 for (
j = 0;
j < dependencies[
i]->
ndeps;
j++)
977 strongest = dependency;
1040 for (
i = 0;
i < ndependencies;
i++)
1070 if (list_attnums[listidx] ==
i)
1072 attr_clauses =
lappend(attr_clauses, clause);
1078 jointype, sjinfo,
false);
1079 attr_sel[attidx++] = simple_sel;
1106 for (
i = ndependencies - 1;
i >= 0;
i--)
1119 s1 *= attr_sel[attidx];
1125 s2 = attr_sel[attidx];
1139 attr_sel[attidx] = f + (1 - f) *
s2;
1141 attr_sel[attidx] = f *
s2 /
s1 + (1 - f) *
s2;
1150 for (
i = 0;
i < nattrs;
i++)
1181 if (rinfo->pseudoconstant)
1271 foreach(lc, bool_expr->
args)
1273 Node *or_expr = NULL;
1280 statlist, &or_expr))
1287 if (!
equal(or_expr, *expr))
1308 clause_expr = (
Node *) clause;
1321 foreach(lc, statlist)
1326 if (info->
kind != STATS_EXT_DEPENDENCIES)
1329 foreach(lc2, info->
exprs)
1333 if (
equal(clause_expr, stat_expr))
1386 int nfunc_dependencies;
1395 Node **unique_exprs;
1396 int unique_exprs_cnt;
1412 unique_exprs_cnt = 0;
1449 list_attnums[listidx] =
attnum;
1461 for (
i = 0;
i < unique_exprs_cnt;
i++)
1463 if (
equal(unique_exprs[
i], expr))
1474 unique_exprs[unique_exprs_cnt++] = expr;
1477 attnum = (-unique_exprs_cnt);
1481 list_attnums[listidx] =
attnum;
1495 if (unique_exprs_cnt > 0)
1496 attnum_offset = (unique_exprs_cnt + 1);
1513 Assert(list_attnums[
i] >= (-unique_exprs_cnt));
1517 attnum = list_attnums[
i] + attnum_offset;
1549 pfree(list_attnums);
1566 nfunc_dependencies = 0;
1578 if (
stat->kind != STATS_EXT_DEPENDENCIES)
1582 if (
stat->inherit != rte->
inh)
1610 for (
i = 0;
i < unique_exprs_cnt;
i++)
1614 foreach(lc,
stat->exprs)
1619 if (
equal(stat_expr, unique_exprs[
i]))
1628 if (nmatched + nexprs < 2)
1659 if (unique_exprs_cnt > 0 ||
stat->exprs !=
NIL)
1720 for (
int m = 0; m < unique_exprs_cnt; m++)
1726 if (
equal(unique_exprs[m], expr))
1728 unique_attnum = -(m + 1) + attnum_offset;
1759 deps->
ndeps = ndeps;
1766 if (deps->
ndeps > 0)
1768 func_dependencies[nfunc_dependencies] = deps;
1769 total_ndeps += deps->
ndeps;
1770 nfunc_dependencies++;
1775 if (nfunc_dependencies == 0)
1777 pfree(func_dependencies);
1779 pfree(list_attnums);
1780 pfree(unique_exprs);
1804 dependencies[ndependencies++] = dependency;
1815 if (ndependencies != 0)
1817 sjinfo, dependencies, ndependencies,
1818 list_attnums, estimatedclauses);
1821 for (
i = 0;
i < nfunc_dependencies;
i++)
1822 pfree(func_dependencies[
i]);
1824 pfree(dependencies);
1825 pfree(func_dependencies);
1827 pfree(list_attnums);
1828 pfree(unique_exprs);
Datum idx(PG_FUNCTION_ARGS)
#define AttributeNumberIsValid(attributeNumber)
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
#define InvalidAttrNumber
int bms_next_member(const Bitmapset *a, int prevbit)
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_del_member(Bitmapset *a, int x)
BMS_Membership bms_membership(const Bitmapset *a)
int bms_member_index(Bitmapset *a, int x)
bool is_pseudo_constant_clause(Node *clause)
Selectivity clauselist_selectivity_ext(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, bool use_extended_stats)
elog(ERROR, "%s: %s", p2, msg)
MVDependencies * statext_dependencies_deserialize(bytea *data)
static void generate_dependencies(DependencyGenerator state)
Datum pg_dependencies_in(PG_FUNCTION_ARGS)
static bool dependency_is_compatible_expression(Node *clause, Index relid, List *statlist, Node **expr)
static AttrNumber * DependencyGenerator_next(DependencyGenerator state)
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
Datum pg_dependencies_out(PG_FUNCTION_ARGS)
static bool dependency_is_compatible_clause(Node *clause, Index relid, AttrNumber *attnum)
static bool dependency_is_fully_matched(MVDependency *dependency, Bitmapset *attnums)
static void DependencyGenerator_free(DependencyGenerator state)
struct DependencyGeneratorData DependencyGeneratorData
MVDependencies * statext_dependencies_load(Oid mvoid, bool inh)
static Selectivity clauselist_apply_dependencies(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, MVDependency **dependencies, int ndependencies, AttrNumber *list_attnums, Bitmapset **estimatedclauses)
Datum pg_dependencies_send(PG_FUNCTION_ARGS)
static DependencyGenerator DependencyGenerator_init(int n, int k)
#define SizeOfItem(natts)
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
MVDependencies * statext_dependencies_build(StatsBuildData *data)
static double dependency_degree(StatsBuildData *data, int k, AttrNumber *dependency)
Datum pg_dependencies_recv(PG_FUNCTION_ARGS)
static void generate_dependencies_recurse(DependencyGenerator state, int index, AttrNumber start, AttrNumber *current)
DependencyGeneratorData * DependencyGenerator
static MVDependency * find_strongest_dependency(MVDependencies **dependencies, int ndependencies, Bitmapset *attnums)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
bool has_stats_of_kind(List *stats, char requiredkind)
int multi_sort_compare_dims(int start, int end, const SortItem *a, const SortItem *b, MultiSortSupport mss)
int multi_sort_compare_dim(int dim, const SortItem *a, const SortItem *b, MultiSortSupport mss)
SortItem * build_sorted_items(StatsBuildData *data, int *nitems, MultiSortSupport mss, int numattrs, AttrNumber *attnums)
MultiSortSupport multi_sort_init(int ndims)
void multi_sort_add_dimension(MultiSortSupport mss, int sortdim, Oid oper, Oid collation)
#define PG_GETARG_BYTEA_PP(n)
#define DatumGetByteaPP(X)
#define PG_RETURN_CSTRING(x)
#define HeapTupleIsValid(tuple)
#define MaxHeapAttributeNumber
if(TABLE==NULL||TABLE_index==NULL)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
RegProcedure get_oprrest(Oid opno)
void MemoryContextReset(MemoryContext context)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
void * repalloc(void *pointer, Size size)
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
static Expr * get_notclausearg(const void *notclause)
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)
#define planner_rt_fetch(rti, root)
static const struct exclude_list_item skip[]
static int list_length(const List *l)
static void * list_nth(const List *list, int n)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
#define CLAMP_PROBABILITY(p)
#define STATS_MAX_DIMENSIONS
#define STATS_DEPS_TYPE_BASIC
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
AttrNumber * dependencies
MVDependency * deps[FLEXIBLE_ARRAY_MEMBER]
AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]
void ReleaseSysCache(HeapTuple tuple)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define SET_VARSIZE(PTR, len)
#define VARSIZE_ANY_EXHDR(PTR)
Datum byteasend(PG_FUNCTION_ARGS)