mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
Collect and use multi-column dependency stats
Follow on patch in the multi-variate statistics patch series.
CREATE STATISTICS s1 WITH (dependencies) ON (a, b) FROM t;
ANALYZE;
will collect dependency stats on (a, b) and then use the measured
dependency in subsequent query planning.
Commit 7b504eb282
added
CREATE STATISTICS with n-distinct coefficients. These are now
specified using the mutually exclusive option WITH (ndistinct).
Author: Tomas Vondra, David Rowley
Reviewed-by: Kyotaro HORIGUCHI, Álvaro Herrera, Dean Rasheed, Robert Haas
and many other comments and contributions
Discussion: https://postgr.es/m/56f40b20-c464-fad2-ff39-06b668fac47c@2ndquadrant.com
This commit is contained in:
@ -1452,6 +1452,13 @@ pg_get_statisticsext_worker(Oid statextid, bool missing_ok)
|
||||
StringInfoData buf;
|
||||
int colno;
|
||||
char *nsp;
|
||||
ArrayType *arr;
|
||||
char *enabled;
|
||||
Datum datum;
|
||||
bool isnull;
|
||||
bool ndistinct_enabled;
|
||||
bool dependencies_enabled;
|
||||
int i;
|
||||
|
||||
statexttup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statextid));
|
||||
|
||||
@ -1467,10 +1474,55 @@ pg_get_statisticsext_worker(Oid statextid, bool missing_ok)
|
||||
initStringInfo(&buf);
|
||||
|
||||
nsp = get_namespace_name(statextrec->stanamespace);
|
||||
appendStringInfo(&buf, "CREATE STATISTICS %s ON (",
|
||||
appendStringInfo(&buf, "CREATE STATISTICS %s",
|
||||
quote_qualified_identifier(nsp,
|
||||
NameStr(statextrec->staname)));
|
||||
|
||||
/*
|
||||
* Lookup the staenabled column so that we know how to handle the WITH
|
||||
* clause.
|
||||
*/
|
||||
datum = SysCacheGetAttr(STATEXTOID, statexttup,
|
||||
Anum_pg_statistic_ext_staenabled, &isnull);
|
||||
Assert(!isnull);
|
||||
arr = DatumGetArrayTypeP(datum);
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != CHAROID)
|
||||
elog(ERROR, "staenabled is not a 1-D char array");
|
||||
enabled = (char *) ARR_DATA_PTR(arr);
|
||||
|
||||
ndistinct_enabled = false;
|
||||
dependencies_enabled = false;
|
||||
|
||||
for (i = 0; i < ARR_DIMS(arr)[0]; i++)
|
||||
{
|
||||
if (enabled[i] == STATS_EXT_NDISTINCT)
|
||||
ndistinct_enabled = true;
|
||||
if (enabled[i] == STATS_EXT_DEPENDENCIES)
|
||||
dependencies_enabled = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* If any option is disabled, then we'll need to append a WITH clause to
|
||||
* show which options are enabled. We omit the WITH clause on purpose
|
||||
* when all options are enabled, so a pg_dump/pg_restore will create all
|
||||
* statistics types on a newer postgres version, if the statistics had all
|
||||
* options enabled on the original version.
|
||||
*/
|
||||
if (!ndistinct_enabled || !dependencies_enabled)
|
||||
{
|
||||
appendStringInfoString(&buf, " WITH (");
|
||||
if (ndistinct_enabled)
|
||||
appendStringInfoString(&buf, "ndistinct");
|
||||
else if (dependencies_enabled)
|
||||
appendStringInfoString(&buf, "dependencies");
|
||||
|
||||
appendStringInfoChar(&buf, ')');
|
||||
}
|
||||
|
||||
appendStringInfoString(&buf, " ON (");
|
||||
|
||||
for (colno = 0; colno < statextrec->stakeys.dim1; colno++)
|
||||
{
|
||||
AttrNumber attnum = statextrec->stakeys.values[colno];
|
||||
|
Reference in New Issue
Block a user