postgresql/src/include/catalog/pg_statistic_ext.h
Tomas Vondra a4d75c86bf 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
2021-03-27 00:01:11 +01:00

91 lines
3 KiB
C

/*-------------------------------------------------------------------------
*
* pg_statistic_ext.h
* definition of the "extended statistics" system catalog
* (pg_statistic_ext)
*
* Note that pg_statistic_ext contains the definitions of extended statistics
* objects, created by CREATE STATISTICS, but not the actual statistical data,
* created by running ANALYZE.
*
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/catalog/pg_statistic_ext.h
*
* NOTES
* The Catalog.pm module reads this file and derives schema
* information.
*
*-------------------------------------------------------------------------
*/
#ifndef PG_STATISTIC_EXT_H
#define PG_STATISTIC_EXT_H
#include "catalog/genbki.h"
#include "catalog/pg_statistic_ext_d.h"
/* ----------------
* pg_statistic_ext definition. cpp turns this into
* typedef struct FormData_pg_statistic_ext
* ----------------
*/
CATALOG(pg_statistic_ext,3381,StatisticExtRelationId)
{
Oid oid; /* oid */
Oid stxrelid BKI_LOOKUP(pg_class); /* relation containing
* attributes */
/* These two fields form the unique key for the entry: */
NameData stxname; /* statistics object name */
Oid stxnamespace BKI_LOOKUP(pg_namespace); /* OID of statistics
* object's namespace */
Oid stxowner BKI_LOOKUP(pg_authid); /* statistics object's owner */
int32 stxstattarget BKI_DEFAULT(-1); /* statistics target */
/*
* variable-length fields start here, but we allow direct access to
* stxkeys
*/
int2vector stxkeys BKI_FORCE_NOT_NULL; /* array of column keys */
#ifdef CATALOG_VARLEN
char stxkind[1] BKI_FORCE_NOT_NULL; /* statistics kinds requested
* to build */
pg_node_tree stxexprs; /* A list of expression trees for stats
* attributes that are not simple column
* references. */
#endif
} FormData_pg_statistic_ext;
/* ----------------
* Form_pg_statistic_ext corresponds to a pointer to a tuple with
* the format of pg_statistic_ext relation.
* ----------------
*/
typedef FormData_pg_statistic_ext *Form_pg_statistic_ext;
DECLARE_TOAST(pg_statistic_ext, 3439, 3440);
DECLARE_UNIQUE_INDEX_PKEY(pg_statistic_ext_oid_index, 3380, on pg_statistic_ext using btree(oid oid_ops));
#define StatisticExtOidIndexId 3380
DECLARE_UNIQUE_INDEX(pg_statistic_ext_name_index, 3997, on pg_statistic_ext using btree(stxname name_ops, stxnamespace oid_ops));
#define StatisticExtNameIndexId 3997
DECLARE_INDEX(pg_statistic_ext_relid_index, 3379, on pg_statistic_ext using btree(stxrelid oid_ops));
#define StatisticExtRelidIndexId 3379
DECLARE_ARRAY_FOREIGN_KEY((stxrelid, stxkeys), pg_attribute, (attrelid, attnum));
#ifdef EXPOSE_TO_CLIENT_CODE
#define STATS_EXT_NDISTINCT 'd'
#define STATS_EXT_DEPENDENCIES 'f'
#define STATS_EXT_MCV 'm'
#define STATS_EXT_EXPRESSIONS 'e'
#endif /* EXPOSE_TO_CLIENT_CODE */
#endif /* PG_STATISTIC_EXT_H */