CREATE STATISTICS
Defines extended statistics.
Synopsis
CREATE STATISTICS [ IF NOT EXISTS ] <statistics_name>
[ ( <statistics_kind> [, ... ] ) ]
ON <column_name>, <column_name> [, ...]
FROM <table_name>
Description
CREATE STATISTICS
creates a new extended statistics object tracking data about the specified table, foreign table, or materialized view. The statistics object is created in the current database and will be owned by the user issuing the command.
If a schema name is given (for example, CREATE STATISTICS myschema.mystat ...
) then the statistics object is created in the specified schema. Otherwise it is created in the current schema. The name of the statistics object must be distinct from the name of any other statistics object in the same schema.
Parameters
IF NOT EXISTS
Do not throw an error if a statistics object with the same name already exists. Apache Cloudberry issues a notice in this case. Note that only the name of the statistics object is considered here, not the details of its definition.
statistics_name
The name (optionally schema-qualified) of the statistics object to create.
statistics_kind
A statistics kind to be computed in this statistics object. Currently supported kinds are ndistinct
, which enables n-distinct statistics, dependencies
, which enables functional dependency statistics, and mcv
which enables most-common values lists. If this clause is omitted, all supported statistics kinds are included in the statistics object.
column_name
The name of a table column to be covered by the computed statistics. You must specify at least two column names; the order of the column names is insignificant.
table_name
The name (optionally schema-qualified) of the table containing the column(s) on which the statistics are computed; see ANALYZE for an explanation of inheritance and partition handling.
Notes
You must be the owner of a table to create a statistics object that reads it. Once created, however, the ownership of the statistics object is independent of the underlying table(s).
Examples
Create table t1
with two functionally-dependent columns, i.e., knowledge of a value in the first column is sufficient for determining the value in the other column. Then build functional dependency statistics on those columns:
CREATE TABLE t1 (
a int,
b int
);
INSERT INTO t1 SELECT i/100, i/500
FROM generate_series(1,1000000) s(i);
ANALYZE t1;
-- the number of matching rows will be drastically underestimated:
EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);
CREATE STATISTICS s1 (dependencies) ON a, b FROM t1;
ANALYZE t1;
-- now the row count estimate is more accurate:
EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);