mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
Implement multivariate n-distinct coefficients
Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
This commit is contained in:
@ -29,6 +29,7 @@
|
||||
#include "catalog/heap.h"
|
||||
#include "catalog/partition.h"
|
||||
#include "catalog/pg_am.h"
|
||||
#include "catalog/pg_statistic_ext.h"
|
||||
#include "foreign/fdwapi.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/makefuncs.h"
|
||||
@ -40,8 +41,11 @@
|
||||
#include "parser/parse_relation.h"
|
||||
#include "parser/parsetree.h"
|
||||
#include "rewrite/rewriteManip.h"
|
||||
#include "statistics/statistics.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/syscache.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/snapmgr.h"
|
||||
|
||||
@ -63,7 +67,7 @@ static List *get_relation_constraints(PlannerInfo *root,
|
||||
bool include_notnull);
|
||||
static List *build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
|
||||
Relation heapRelation);
|
||||
|
||||
static List *get_relation_statistics(RelOptInfo *rel, Relation relation);
|
||||
|
||||
/*
|
||||
* get_relation_info -
|
||||
@ -398,6 +402,8 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
|
||||
|
||||
rel->indexlist = indexinfos;
|
||||
|
||||
rel->statlist = get_relation_statistics(rel, relation);
|
||||
|
||||
/* Grab foreign-table info using the relcache, while we have it */
|
||||
if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
|
||||
{
|
||||
@ -1251,6 +1257,65 @@ get_relation_constraints(PlannerInfo *root,
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* get_relation_statistics
|
||||
* Retrieve extended statistics defined on the table.
|
||||
*
|
||||
* Returns a List (possibly empty) of StatisticExtInfo objects describing
|
||||
* the statistics. Note that this doesn't load the actual statistics data,
|
||||
* just the identifying metadata. Only stats actually built are considered.
|
||||
*/
|
||||
static List *
|
||||
get_relation_statistics(RelOptInfo *rel, Relation relation)
|
||||
{
|
||||
List *statoidlist;
|
||||
List *stainfos = NIL;
|
||||
ListCell *l;
|
||||
|
||||
statoidlist = RelationGetStatExtList(relation);
|
||||
|
||||
foreach(l, statoidlist)
|
||||
{
|
||||
Oid statOid = lfirst_oid(l);
|
||||
Form_pg_statistic_ext staForm;
|
||||
HeapTuple htup;
|
||||
Bitmapset *keys = NULL;
|
||||
int i;
|
||||
|
||||
htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statOid));
|
||||
if (!htup)
|
||||
elog(ERROR, "cache lookup failed for statistics %u", statOid);
|
||||
staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
|
||||
|
||||
/*
|
||||
* First, build the array of columns covered. This is ultimately
|
||||
* wasted if no stats are actually built, but it doesn't seem worth
|
||||
* troubling over that case.
|
||||
*/
|
||||
for (i = 0; i < staForm->stakeys.dim1; i++)
|
||||
keys = bms_add_member(keys, staForm->stakeys.values[i]);
|
||||
|
||||
/* add one StatisticExtInfo for each kind built */
|
||||
if (statext_is_kind_built(htup, STATS_EXT_NDISTINCT))
|
||||
{
|
||||
StatisticExtInfo *info = makeNode(StatisticExtInfo);
|
||||
|
||||
info->statOid = statOid;
|
||||
info->rel = rel;
|
||||
info->kind = STATS_EXT_NDISTINCT;
|
||||
info->keys = bms_copy(keys);
|
||||
|
||||
stainfos = lcons(info, stainfos);
|
||||
}
|
||||
|
||||
ReleaseSysCache(htup);
|
||||
bms_free(keys);
|
||||
}
|
||||
|
||||
list_free(statoidlist);
|
||||
|
||||
return stainfos;
|
||||
}
|
||||
|
||||
/*
|
||||
* relation_excluded_by_constraints
|
||||
|
Reference in New Issue
Block a user