pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Extended statistics on expressions
Date: Sat, 27 Mar 2021 00:14:06 +0000
Message-ID: <E1lPwac-00034Z-2O@gemulon.postgresql.org> (raw)

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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a4d75c86bf15220df22de0a92c819ecef9db3849

Modified Files
--------------
doc/src/sgml/catalogs.sgml                       |  295 +++-
doc/src/sgml/ref/create_statistics.sgml          |  116 +-
src/backend/catalog/Makefile                     |    8 +-
src/backend/catalog/system_views.sql             |   69 +
src/backend/commands/statscmds.c                 |  437 +++---
src/backend/commands/tablecmds.c                 |  104 +-
src/backend/nodes/copyfuncs.c                    |   14 +
src/backend/nodes/equalfuncs.c                   |   13 +
src/backend/nodes/outfuncs.c                     |   12 +
src/backend/optimizer/util/plancat.c             |   62 +
src/backend/parser/gram.y                        |   38 +-
src/backend/parser/parse_agg.c                   |   10 +
src/backend/parser/parse_expr.c                  |    6 +
src/backend/parser/parse_func.c                  |    3 +
src/backend/parser/parse_utilcmd.c               |  125 +-
src/backend/statistics/dependencies.c            |  616 +++++++-
src/backend/statistics/extended_stats.c          | 1253 ++++++++++++++--
src/backend/statistics/mcv.c                     |  369 +++--
src/backend/statistics/mvdistinct.c              |   96 +-
src/backend/tcop/utility.c                       |   29 +-
src/backend/utils/adt/ruleutils.c                |  281 +++-
src/backend/utils/adt/selfuncs.c                 |  413 +++++-
src/bin/pg_dump/t/002_pg_dump.pl                 |   12 +
src/bin/psql/describe.c                          |  130 +-
src/include/catalog/catversion.h                 |    2 +-
src/include/catalog/pg_proc.dat                  |    8 +
src/include/catalog/pg_statistic_ext.h           |    4 +
src/include/catalog/pg_statistic_ext_data.h      |    1 +
src/include/commands/defrem.h                    |    4 +-
src/include/nodes/nodes.h                        |    1 +
src/include/nodes/parsenodes.h                   |   19 +-
src/include/nodes/pathnodes.h                    |    1 +
src/include/parser/parse_node.h                  |    1 +
src/include/parser/parse_utilcmd.h               |    2 +
src/include/statistics/extended_stats_internal.h |   32 +-
src/include/statistics/statistics.h              |    5 +-
src/include/utils/ruleutils.h                    |    2 +
src/test/regress/expected/create_table_like.out  |   20 +-
src/test/regress/expected/oidjoins.out           |   10 +-
src/test/regress/expected/rules.out              |   73 +
src/test/regress/expected/stats_ext.out          | 1658 +++++++++++++++++++---
src/test/regress/sql/create_table_like.sql       |    2 +
src/test/regress/sql/stats_ext.sql               |  589 +++++++-
43 files changed, 5982 insertions(+), 963 deletions(-)



view thread (10+ messages)  latest in thread

Message-ID: <E1lPwac-00034Z-2O@gemulon.postgresql.org>
Permalink:  ../E1lPwac-00034Z-2O@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1lPwac-00034Z-2O@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tomas.vondra@postgresql.org
  Subject: Re: pgsql: Extended statistics on expressions
  In-Reply-To: <E1lPwac-00034Z-2O@gemulon.postgresql.org>
  References: <E1lPwac-00034Z-2O@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox