diff options
author | Tomas Vondra | 2021-03-26 22:22:01 +0000 |
---|---|---|
committer | Tomas Vondra | 2021-03-26 23:01:11 +0000 |
commit | a4d75c86bf15220df22de0a92c819ecef9db3849 (patch) | |
tree | a736a68b1c3f022590a886b7bac45276f1f490a6 /src/bin/psql/describe.c | |
parent | 98376c18f12e562421b5c77e619248e8b7aae3c6 (diff) |
Extended statistics on expressions
Allow defining extended statistics on expressions, not just just on
simple column references. With this commit, expressions are supported
by all existing extended statistics kinds, improving the same types of
estimates. A simple example may look like this:
CREATE TABLE t (a int);
CREATE STATISTICS s ON mod(a,10), mod(a,20) FROM t;
ANALYZE t;
The collected statistics are useful e.g. to estimate queries with those
expressions in WHERE or GROUP BY clauses:
SELECT * FROM t WHERE mod(a,10) = 0 AND mod(a,20) = 0;
SELECT 1 FROM t GROUP BY mod(a,10), mod(a,20);
This introduces new internal statistics kind 'e' (expressions) which is
built automatically when the statistics object definition includes any
expressions. This represents single-expression statistics, as if there
was an expression index (but without the index maintenance overhead).
The statistics is stored in pg_statistics_ext_data as an array of
composite types, which is possible thanks to 79f6a942bd.
CREATE STATISTICS allows building statistics on a single expression, in
which case in which case it's not possible to specify statistics kinds.
A new system view pg_stats_ext_exprs can be used to display expression
statistics, similarly to pg_stats and pg_stats_ext views.
ALTER TABLE ... ALTER COLUMN ... TYPE now treats indexes the same way it
treats indexes, i.e. it drops and recreates the statistics. This means
all statistics are reset, and we no longer try to preserve at least the
functional dependencies. This should not be a major issue in practice,
as the functional dependencies actually rely on per-column statistics,
which were always reset anyway.
Author: Tomas Vondra
Reviewed-by: Justin Pryzby, Dean Rasheed, Zhihong Yu
Discussion: https://postgr.es/m/ad7891d2-e90c-b446-9fe2-7419143847d7%40enterprisedb.com
Diffstat (limited to 'src/bin/psql/describe.c')
-rw-r--r-- | src/bin/psql/describe.c | 130 |
1 files changed, 118 insertions, 12 deletions
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index e56cc43e111..440249ff69d 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2712,7 +2712,104 @@ describeOneTableDetails(const char *schemaname, } /* print any extended statistics */ - if (pset.sversion >= 100000) + if (pset.sversion >= 140000) + { + printfPQExpBuffer(&buf, + "SELECT oid, " + "stxrelid::pg_catalog.regclass, " + "stxnamespace::pg_catalog.regnamespace AS nsp, " + "stxname,\n" + "pg_get_statisticsobjdef_columns(oid) AS columns,\n" + " 'd' = any(stxkind) AS ndist_enabled,\n" + " 'f' = any(stxkind) AS deps_enabled,\n" + " 'm' = any(stxkind) AS mcv_enabled,\n" + "stxstattarget\n" + "FROM pg_catalog.pg_statistic_ext stat\n" + "WHERE stxrelid = '%s'\n" + "ORDER BY 1;", + oid); + + result = PSQLexec(buf.data); + if (!result) + goto error_return; + else + tuples = PQntuples(result); + + if (tuples > 0) + { + printTableAddFooter(&cont, _("Statistics objects:")); + + for (i = 0; i < tuples; i++) + { + bool gotone = false; + bool has_ndistinct; + bool has_dependencies; + bool has_mcv; + bool has_all; + bool has_some; + + has_ndistinct = (strcmp(PQgetvalue(result, i, 5), "t") == 0); + has_dependencies = (strcmp(PQgetvalue(result, i, 6), "t") == 0); + has_mcv = (strcmp(PQgetvalue(result, i, 7), "t") == 0); + + printfPQExpBuffer(&buf, " "); + + /* statistics object name (qualified with namespace) */ + appendPQExpBuffer(&buf, "\"%s\".\"%s\"", + PQgetvalue(result, i, 2), + PQgetvalue(result, i, 3)); + + /* + * When printing kinds we ignore expression statistics, + * which is used only internally and can't be specified by + * user. We don't print the kinds when either none are + * specified (in which case it has to be statistics on a + * single expr) or when all are specified (in which case + * we assume it's expanded by CREATE STATISTICS). + */ + has_all = (has_ndistinct && has_dependencies && has_mcv); + has_some = (has_ndistinct || has_dependencies || has_mcv); + + if (has_some && !has_all) + { + appendPQExpBuffer(&buf, " ("); + + /* options */ + if (has_ndistinct) + { + appendPQExpBufferStr(&buf, "ndistinct"); + gotone = true; + } + + if (has_dependencies) + { + appendPQExpBuffer(&buf, "%sdependencies", gotone ? ", " : ""); + gotone = true; + } + + if (has_mcv) + { + appendPQExpBuffer(&buf, "%smcv", gotone ? ", " : ""); + } + + appendPQExpBuffer(&buf, ")"); + } + + appendPQExpBuffer(&buf, " ON %s FROM %s", + PQgetvalue(result, i, 4), + PQgetvalue(result, i, 1)); + + /* Show the stats target if it's not default */ + if (strcmp(PQgetvalue(result, i, 8), "-1") != 0) + appendPQExpBuffer(&buf, "; STATISTICS %s", + PQgetvalue(result, i, 8)); + + printTableAddFooter(&cont, buf.data); + } + } + PQclear(result); + } + else if (pset.sversion >= 100000) { printfPQExpBuffer(&buf, "SELECT oid, " @@ -4468,18 +4565,27 @@ listExtendedStats(const char *pattern) printfPQExpBuffer(&buf, "SELECT \n" "es.stxnamespace::pg_catalog.regnamespace::text AS \"%s\", \n" - "es.stxname AS \"%s\", \n" - "pg_catalog.format('%%s FROM %%s', \n" - " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(a.attname),', ') \n" - " FROM pg_catalog.unnest(es.stxkeys) s(attnum) \n" - " JOIN pg_catalog.pg_attribute a \n" - " ON (es.stxrelid = a.attrelid \n" - " AND a.attnum = s.attnum \n" - " AND NOT a.attisdropped)), \n" - "es.stxrelid::regclass) AS \"%s\"", + "es.stxname AS \"%s\", \n", gettext_noop("Schema"), - gettext_noop("Name"), - gettext_noop("Definition")); + gettext_noop("Name")); + + if (pset.sversion >= 140000) + appendPQExpBuffer(&buf, + "pg_catalog.format('%%s FROM %%s', \n" + " pg_get_statisticsobjdef_columns(es.oid), \n" + " es.stxrelid::regclass) AS \"%s\"", + gettext_noop("Definition")); + else + appendPQExpBuffer(&buf, + "pg_catalog.format('%%s FROM %%s', \n" + " (SELECT pg_catalog.string_agg(pg_catalog.quote_ident(a.attname),', ') \n" + " FROM pg_catalog.unnest(es.stxkeys) s(attnum) \n" + " JOIN pg_catalog.pg_attribute a \n" + " ON (es.stxrelid = a.attrelid \n" + " AND a.attnum = s.attnum \n" + " AND NOT a.attisdropped)), \n" + "es.stxrelid::regclass) AS \"%s\"", + gettext_noop("Definition")); appendPQExpBuffer(&buf, ",\nCASE WHEN 'd' = any(es.stxkind) THEN 'defined' \n" |