28 #include "utils/fmgroids.h"
29 #include "utils/fmgrprotos.h"
38 #define SizeOfHeader (3 * sizeof(uint32))
41 #define SizeOfItem(natts) \
42 (sizeof(double) + sizeof(AttrNumber) * (1 + (natts)))
45 #define MinSizeOfItem SizeOfItem(2)
48 #define MinSizeOfItems(ndeps) \
49 (SizeOfHeader + (ndeps) * MinSizeOfItem)
149 state->ndependencies++;
177 Assert((n >= k) && (k > 0));
183 state->ndependencies = 0;
231 int n_violations = 0;
234 int n_supporting_rows = 0;
247 for (
i = 0;
i < k;
i++)
248 attnums_dep[
i] =
data->attnums[dependency[
i]];
271 elog(
ERROR,
"cache lookup failed for ordering operator for type %u",
312 if (n_violations == 0)
313 n_supporting_rows += group_size;
328 return (n_supporting_rows * 1.0 /
data->numrows);
361 "dependency_degree cxt",
370 for (k = 2; k <=
data->nattnums; k++)
405 for (
i = 0;
i < k;
i++)
409 if (dependencies == NULL)
416 dependencies->
ndeps = 0;
419 dependencies->
ndeps++;
424 dependencies->
deps[dependencies->
ndeps - 1] = d;
455 for (
i = 0;
i < dependencies->
ndeps;
i++)
466 memcpy(tmp, &dependencies->
type,
sizeof(
uint32));
472 for (
i = 0;
i < dependencies->
ndeps;
i++)
476 memcpy(tmp, &d->
degree,
sizeof(
double));
477 tmp +=
sizeof(double);
502 Size min_expected_size;
510 elog(
ERROR,
"invalid MVDependencies size %zu (expected at least %zu)",
522 memcpy(&dependencies->
type, tmp,
sizeof(
uint32));
528 elog(
ERROR,
"invalid dependency magic %d (expected %d)",
532 elog(
ERROR,
"invalid dependency type %d (expected %d)",
535 if (dependencies->
ndeps == 0)
536 elog(
ERROR,
"invalid zero-length item array in MVDependencies");
542 elog(
ERROR,
"invalid dependencies size %zu (expected at least %zu)",
549 for (
i = 0;
i < dependencies->
ndeps;
i++)
556 memcpy(°ree, tmp,
sizeof(
double));
557 tmp +=
sizeof(double);
577 dependencies->
deps[
i] = d;
630 elog(
ERROR,
"cache lookup failed for statistics object %u", mvoid);
633 Anum_pg_statistic_ext_data_stxddependencies, &isnull);
636 "requested statistics kind \"%c\" is not yet built for statistics object %u",
637 STATS_EXT_DEPENDENCIES, mvoid);
660 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
661 errmsg(
"cannot accept a value of type %s",
"pg_dependencies")));
681 for (
i = 0;
i < dependencies->
ndeps;
i++)
713 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
714 errmsg(
"cannot accept a value of type %s",
"pg_dependencies")));
751 if (rinfo->pseudoconstant)
842 foreach(lc, bool_expr->
args)
851 relid, &clause_attnum))
858 if (*
attnum != clause_attnum)
879 clause_expr = (
Node *) clause;
890 if (!
IsA(clause_expr,
Var))
894 var = (
Var *) clause_expr;
897 if (var->
varno != relid)
944 for (
i = 0;
i < ndependencies;
i++)
946 for (
j = 0;
j < dependencies[
i]->
ndeps;
j++)
975 strongest = dependency;
1038 for (
i = 0;
i < ndependencies;
i++)
1068 if (list_attnums[listidx] ==
i)
1070 attr_clauses =
lappend(attr_clauses, clause);
1076 jointype, sjinfo,
false);
1077 attr_sel[attidx++] = simple_sel;
1104 for (
i = ndependencies - 1;
i >= 0;
i--)
1117 s1 *= attr_sel[attidx];
1123 s2 = attr_sel[attidx];
1137 attr_sel[attidx] = f + (1 - f) *
s2;
1139 attr_sel[attidx] = f *
s2 /
s1 + (1 - f) *
s2;
1148 for (
i = 0;
i < nattrs;
i++)
1179 if (rinfo->pseudoconstant)
1269 foreach(lc, bool_expr->
args)
1271 Node *or_expr = NULL;
1278 statlist, &or_expr))
1285 if (!
equal(or_expr, *expr))
1306 clause_expr = (
Node *) clause;
1319 foreach(lc, statlist)
1324 if (info->
kind != STATS_EXT_DEPENDENCIES)
1327 foreach(lc2, info->
exprs)
1331 if (
equal(clause_expr, stat_expr))
1384 int nfunc_dependencies;
1393 Node **unique_exprs;
1394 int unique_exprs_cnt;
1410 unique_exprs_cnt = 0;
1447 list_attnums[listidx] =
attnum;
1459 for (
i = 0;
i < unique_exprs_cnt;
i++)
1461 if (
equal(unique_exprs[
i], expr))
1472 unique_exprs[unique_exprs_cnt++] = expr;
1475 attnum = (-unique_exprs_cnt);
1479 list_attnums[listidx] =
attnum;
1493 if (unique_exprs_cnt > 0)
1494 attnum_offset = (unique_exprs_cnt + 1);
1511 Assert(list_attnums[
i] >= (-unique_exprs_cnt));
1515 attnum = list_attnums[
i] + attnum_offset;
1547 pfree(list_attnums);
1564 nfunc_dependencies = 0;
1576 if (
stat->kind != STATS_EXT_DEPENDENCIES)
1580 if (
stat->inherit != rte->
inh)
1608 for (
i = 0;
i < unique_exprs_cnt;
i++)
1612 foreach(lc,
stat->exprs)
1617 if (
equal(stat_expr, unique_exprs[
i]))
1626 if (nmatched + nexprs < 2)
1657 if (unique_exprs_cnt > 0 ||
stat->exprs !=
NIL)
1718 for (
int m = 0; m < unique_exprs_cnt; m++)
1724 if (
equal(unique_exprs[m], expr))
1726 unique_attnum = -(m + 1) + attnum_offset;
1757 deps->
ndeps = ndeps;
1764 if (deps->
ndeps > 0)
1766 func_dependencies[nfunc_dependencies] = deps;
1767 total_ndeps += deps->
ndeps;
1768 nfunc_dependencies++;
1773 if (nfunc_dependencies == 0)
1775 pfree(func_dependencies);
1777 pfree(list_attnums);
1778 pfree(unique_exprs);
1802 dependencies[ndependencies++] = dependency;
1813 if (ndependencies != 0)
1815 sjinfo, dependencies, ndependencies,
1816 list_attnums, estimatedclauses);
1819 for (
i = 0;
i < nfunc_dependencies;
i++)
1820 pfree(func_dependencies[
i]);
1822 pfree(dependencies);
1823 pfree(func_dependencies);
1825 pfree(list_attnums);
1826 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)
#define Assert(condition)
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)
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)
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_)
#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)
MemoryContextSwitchTo(old_ctx)
#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)