mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +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:
@ -35,6 +35,7 @@
|
||||
#include "catalog/pg_operator.h"
|
||||
#include "catalog/pg_partitioned_table.h"
|
||||
#include "catalog/pg_proc.h"
|
||||
#include "catalog/pg_statistic_ext.h"
|
||||
#include "catalog/pg_trigger.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/defrem.h"
|
||||
@ -317,6 +318,7 @@ static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
|
||||
const Oid *excludeOps,
|
||||
bool attrsOnly, bool showTblSpc,
|
||||
int prettyFlags, bool missing_ok);
|
||||
static char *pg_get_statisticsext_worker(Oid statextid, bool missing_ok);
|
||||
static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags,
|
||||
bool attrsOnly);
|
||||
static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
|
||||
@ -1421,6 +1423,85 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
|
||||
return buf.data;
|
||||
}
|
||||
|
||||
/*
|
||||
* pg_get_statisticsextdef
|
||||
* Get the definition of an extended statistics object
|
||||
*/
|
||||
Datum
|
||||
pg_get_statisticsextdef(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid statextid = PG_GETARG_OID(0);
|
||||
char *res;
|
||||
|
||||
res = pg_get_statisticsext_worker(statextid, true);
|
||||
|
||||
if (res == NULL)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
PG_RETURN_TEXT_P(string_to_text(res));
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal workhorse to decompile an extended statistics object.
|
||||
*/
|
||||
static char *
|
||||
pg_get_statisticsext_worker(Oid statextid, bool missing_ok)
|
||||
{
|
||||
Form_pg_statistic_ext statextrec;
|
||||
Form_pg_class pgclassrec;
|
||||
HeapTuple statexttup;
|
||||
HeapTuple pgclasstup;
|
||||
StringInfoData buf;
|
||||
int colno;
|
||||
|
||||
statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
|
||||
|
||||
if (!HeapTupleIsValid(statexttup))
|
||||
{
|
||||
if (missing_ok)
|
||||
return NULL;
|
||||
elog(ERROR, "cache lookup failed for extended statistics %u", statextid);
|
||||
}
|
||||
|
||||
statextrec = (Form_pg_statistic_ext) GETSTRUCT(statexttup);
|
||||
|
||||
pgclasstup = SearchSysCache1(RELOID, ObjectIdGetDatum(statextrec->starelid));
|
||||
|
||||
if (!HeapTupleIsValid(statexttup))
|
||||
{
|
||||
ReleaseSysCache(statexttup);
|
||||
elog(ERROR, "cache lookup failed for relation %u", statextrec->starelid);
|
||||
}
|
||||
|
||||
pgclassrec = (Form_pg_class) GETSTRUCT(pgclasstup);
|
||||
|
||||
initStringInfo(&buf);
|
||||
|
||||
appendStringInfo(&buf, "CREATE STATISTICS %s ON (",
|
||||
quote_identifier(NameStr(statextrec->staname)));
|
||||
|
||||
for (colno = 0; colno < statextrec->stakeys.dim1; colno++)
|
||||
{
|
||||
AttrNumber attnum = statextrec->stakeys.values[colno];
|
||||
char *attname;
|
||||
|
||||
if (colno > 0)
|
||||
appendStringInfoString(&buf, ", ");
|
||||
|
||||
attname = get_relid_attribute_name(statextrec->starelid, attnum);
|
||||
|
||||
appendStringInfoString(&buf, quote_identifier(attname));
|
||||
}
|
||||
|
||||
appendStringInfo(&buf, ") FROM %s",
|
||||
quote_identifier(NameStr(pgclassrec->relname)));
|
||||
|
||||
ReleaseSysCache(statexttup);
|
||||
ReleaseSysCache(pgclasstup);
|
||||
|
||||
return buf.data;
|
||||
}
|
||||
|
||||
/*
|
||||
* pg_get_partkeydef
|
||||
*
|
||||
|
Reference in New Issue
Block a user