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:
@ -2202,6 +2202,7 @@ _outRelOptInfo(StringInfo str, const RelOptInfo *node)
|
||||
WRITE_NODE_FIELD(lateral_vars);
|
||||
WRITE_BITMAPSET_FIELD(lateral_referencers);
|
||||
WRITE_NODE_FIELD(indexlist);
|
||||
WRITE_NODE_FIELD(statlist);
|
||||
WRITE_UINT_FIELD(pages);
|
||||
WRITE_FLOAT_FIELD(tuples, "%.0f");
|
||||
WRITE_FLOAT_FIELD(allvisfrac, "%.6f");
|
||||
@ -2274,6 +2275,18 @@ _outForeignKeyOptInfo(StringInfo str, const ForeignKeyOptInfo *node)
|
||||
appendStringInfo(str, " %d", list_length(node->rinfos[i]));
|
||||
}
|
||||
|
||||
static void
|
||||
_outStatisticExtInfo(StringInfo str, const StatisticExtInfo *node)
|
||||
{
|
||||
WRITE_NODE_TYPE("STATISTICEXTINFO");
|
||||
|
||||
/* NB: this isn't a complete set of fields */
|
||||
WRITE_OID_FIELD(statOid);
|
||||
/* don't write rel, leads to infinite recursion in plan tree dump */
|
||||
WRITE_CHAR_FIELD(kind);
|
||||
WRITE_BITMAPSET_FIELD(keys);
|
||||
}
|
||||
|
||||
static void
|
||||
_outEquivalenceClass(StringInfo str, const EquivalenceClass *node)
|
||||
{
|
||||
@ -2577,6 +2590,18 @@ _outIndexStmt(StringInfo str, const IndexStmt *node)
|
||||
WRITE_BOOL_FIELD(if_not_exists);
|
||||
}
|
||||
|
||||
static void
|
||||
_outCreateStatsStmt(StringInfo str, const CreateStatsStmt *node)
|
||||
{
|
||||
WRITE_NODE_TYPE("CREATESTATSSTMT");
|
||||
|
||||
WRITE_NODE_FIELD(defnames);
|
||||
WRITE_NODE_FIELD(relation);
|
||||
WRITE_NODE_FIELD(keys);
|
||||
WRITE_NODE_FIELD(options);
|
||||
WRITE_BOOL_FIELD(if_not_exists);
|
||||
}
|
||||
|
||||
static void
|
||||
_outNotifyStmt(StringInfo str, const NotifyStmt *node)
|
||||
{
|
||||
@ -3936,6 +3961,9 @@ outNode(StringInfo str, const void *obj)
|
||||
case T_PlannerParamItem:
|
||||
_outPlannerParamItem(str, obj);
|
||||
break;
|
||||
case T_StatisticExtInfo:
|
||||
_outStatisticExtInfo(str, obj);
|
||||
break;
|
||||
|
||||
case T_ExtensibleNode:
|
||||
_outExtensibleNode(str, obj);
|
||||
@ -3953,6 +3981,9 @@ outNode(StringInfo str, const void *obj)
|
||||
case T_IndexStmt:
|
||||
_outIndexStmt(str, obj);
|
||||
break;
|
||||
case T_CreateStatsStmt:
|
||||
_outCreateStatsStmt(str, obj);
|
||||
break;
|
||||
case T_NotifyStmt:
|
||||
_outNotifyStmt(str, obj);
|
||||
break;
|
||||
|
Reference in New Issue
Block a user