30 #include "utils/fmgroids.h" 31 #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) 98 if (index < (state->
k - 1))
107 for (i = start; i < state->
n; i++)
123 for (i = 0; i < state->
n; i++)
130 for (j = 0; j < index; j++)
177 Assert((n >= k) && (k > 0));
235 int n_violations = 0;
238 int n_supporting_rows = 0;
254 for (i = 0; i <
k; i++)
255 attnums_dep[i] = attnums[dependency[i]];
271 for (i = 0; i <
k; i++)
278 elog(
ERROR,
"cache lookup failed for ordering operator for type %u",
293 mss, k, attnums_dep);
306 for (i = 1; i <= nitems; i++)
320 if (n_violations == 0)
321 n_supporting_rows += group_size;
343 return (n_supporting_rows * 1.0 / numrows);
387 for (k = 2; k <= numattrs; k++)
415 for (i = 0; i <
k; i++)
419 if (dependencies == NULL)
426 dependencies->
ndeps = 0;
429 dependencies->
ndeps++;
434 dependencies->
deps[dependencies->
ndeps - 1] = d;
463 for (i = 0; i < dependencies->
ndeps; i++)
474 memcpy(tmp, &dependencies->
type,
sizeof(
uint32));
480 for (i = 0; i < dependencies->
ndeps; i++)
484 memcpy(tmp, &d->
degree,
sizeof(
double));
485 tmp +=
sizeof(double);
494 Assert(tmp <= ((
char *) output + len));
498 Assert(tmp == ((
char *) output + len));
510 Size min_expected_size;
518 elog(
ERROR,
"invalid MVDependencies size %zd (expected at least %zd)",
530 memcpy(&dependencies->
type, tmp,
sizeof(
uint32));
536 elog(
ERROR,
"invalid dependency magic %d (expected %d)",
540 elog(
ERROR,
"invalid dependency type %d (expected %d)",
543 if (dependencies->
ndeps == 0)
544 elog(
ERROR,
"invalid zero-length item array in MVDependencies");
550 elog(
ERROR,
"invalid dependencies size %zd (expected at least %zd)",
557 for (i = 0; i < dependencies->
ndeps; i++)
564 memcpy(°ree, tmp,
sizeof(
double));
565 tmp +=
sizeof(double);
585 dependencies->
deps[i] = d;
636 elog(
ERROR,
"cache lookup failed for statistics object %u", mvoid);
639 Anum_pg_statistic_ext_data_stxddependencies, &isnull);
642 "requested statistic kind \"%c\" is not yet built for statistics object %u",
643 STATS_EXT_DEPENDENCIES, mvoid);
666 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
667 errmsg(
"cannot accept a value of type %s",
"pg_dependencies")));
687 for (i = 0; i < dependencies->
ndeps; i++)
719 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
720 errmsg(
"cannot accept a value of type %s",
"pg_dependencies")));
847 foreach(lc, expr->
args)
856 relid, &clause_attnum))
860 *attnum = clause_attnum;
862 if (*attnum != clause_attnum)
883 var = (
Var *) clause;
898 if (var->
varno != relid)
947 for (j = 0; j < dependencies[
i]->
ndeps; j++)
976 strongest = dependency;
1069 if (list_attnums[listidx] == i)
1071 attr_clauses =
lappend(attr_clauses, clause);
1077 jointype, sjinfo,
false);
1078 attr_sel[attidx++] = simple_sel;
1105 for (i = ndependencies - 1; i >= 0; i--)
1118 s1 *= attr_sel[attidx];
1124 s2 = attr_sel[attidx];
1138 attr_sel[attidx] = f + (1 - f) * s2;
1140 attr_sel[attidx] = f * s2 / s1 + (1 - f) * s2;
1149 for (i = 0; i < nattrs; i++)
1202 int nfunc_dependencies;
1235 list_attnums[listidx] =
attnum;
1252 pfree(list_attnums);
1269 nfunc_dependencies = 0;
1279 if (stat->
kind != STATS_EXT_DEPENDENCIES)
1290 func_dependencies[nfunc_dependencies]
1293 total_ndeps += func_dependencies[nfunc_dependencies]->
ndeps;
1294 nfunc_dependencies++;
1298 if (nfunc_dependencies == 0)
1300 pfree(func_dependencies);
1302 pfree(list_attnums);
1326 dependencies[ndependencies++] = dependency;
1337 if (ndependencies != 0)
1339 sjinfo, dependencies, ndependencies,
1340 list_attnums, estimatedclauses);
1343 for (i = 0; i < nfunc_dependencies; i++)
1344 pfree(func_dependencies[i]);
1346 pfree(dependencies);
1347 pfree(func_dependencies);
1349 pfree(list_attnums);
AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]
MVDependency * deps[FLEXIBLE_ARRAY_MEMBER]
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
#define IsA(nodeptr, _type_)
MVDependencies * statext_dependencies_load(Oid mvoid)
#define STATS_DEPS_TYPE_BASIC
struct DependencyGeneratorData DependencyGeneratorData
static bool is_orclause(const void *clause)
static void output(uint64 loop_count)
int bms_next_member(const Bitmapset *a, int prevbit)
SortItem * build_sorted_items(int numrows, int *nitems, HeapTuple *rows, TupleDesc tdesc, MultiSortSupport mss, int numattrs, AttrNumber *attnums)
#define SizeOfItem(natts)
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
int multi_sort_compare_dims(int start, int end, const SortItem *a, const SortItem *b, MultiSortSupport mss)
int errcode(int sqlerrcode)
#define DatumGetByteaPP(X)
Datum pg_dependencies_out(PG_FUNCTION_ARGS)
Datum pg_dependencies_recv(PG_FUNCTION_ARGS)
static Selectivity clauselist_apply_dependencies(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, MVDependency **dependencies, int ndependencies, AttrNumber *list_attnums, Bitmapset **estimatedclauses)
#define CLAMP_PROBABILITY(p)
void pfree(void *pointer)
void appendStringInfo(StringInfo str, const char *fmt,...)
#define ObjectIdGetDatum(X)
void multi_sort_add_dimension(MultiSortSupport mss, int sortdim, Oid oper, Oid collation)
static DependencyGenerator DependencyGenerator_init(int n, int k)
static bool dependency_is_fully_matched(MVDependency *dependency, Bitmapset *attnums)
AttrNumber * dependencies
int bms_num_members(const Bitmapset *a)
void appendStringInfoString(StringInfo str, const char *s)
int multi_sort_compare_dim(int dim, const SortItem *a, const SortItem *b, MultiSortSupport mss)
AttrNumber * build_attnums_array(Bitmapset *attrs, int *numattrs)
int bms_member_index(Bitmapset *a, int x)
bool is_pseudo_constant_clause(Node *clause)
DependencyGeneratorData * DependencyGenerator
static void generate_dependencies_recurse(DependencyGenerator state, int index, AttrNumber start, AttrNumber *current)
static void generate_dependencies(DependencyGenerator state)
static double dependency_degree(int numrows, HeapTuple *rows, int k, AttrNumber *dependency, VacAttrStats **stats, Bitmapset *attrs)
MultiSortSupport multi_sort_init(int ndims)
List * lappend(List *list, void *datum)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
Datum byteasend(PG_FUNCTION_ARGS)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
static MVDependency * find_strongest_dependency(MVDependencies **dependencies, int ndependencies, Bitmapset *attnums)
static bool is_notclause(const void *clause)
RegProcedure get_oprrest(Oid opno)
MVDependencies * statext_dependencies_build(int numrows, HeapTuple *rows, Bitmapset *attrs, VacAttrStats **stats)
BMS_Membership bms_membership(const Bitmapset *a)
void * palloc0(Size size)
void ReleaseSysCache(HeapTuple tuple)
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define ereport(elevel,...)
static Expr * get_notclausearg(const void *notclause)
void bms_free(Bitmapset *a)
#define HeapTupleIsValid(tuple)
#define Assert(condition)
Datum pg_dependencies_in(PG_FUNCTION_ARGS)
#define PG_RETURN_CSTRING(x)
static int list_length(const List *l)
#define PG_GETARG_BYTEA_PP(n)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Selectivity clauselist_selectivity_ext(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, bool use_extended_stats)
void * repalloc(void *pointer, Size size)
#define InvalidAttrNumber
#define STATS_MAX_DIMENSIONS
static AttrNumber * DependencyGenerator_next(DependencyGenerator state)
#define VARSIZE_ANY_EXHDR(PTR)
int errmsg(const char *fmt,...)
bytea * statext_dependencies_serialize(MVDependencies *dependencies)
#define SET_VARSIZE(PTR, len)
static bool is_opclause(const void *clause)
Bitmapset * bms_del_member(Bitmapset *a, int x)
bool bms_is_member(int x, const Bitmapset *a)
static void DependencyGenerator_free(DependencyGenerator state)
Datum pg_dependencies_send(PG_FUNCTION_ARGS)
bool has_stats_of_kind(List *stats, char requiredkind)
MVDependencies * statext_dependencies_deserialize(bytea *data)
#define offsetof(type, field)
static bool dependency_is_compatible_clause(Node *clause, Index relid, AttrNumber *attnum)