CREATE_STATISTICS(7pg15)

๐Ÿ“ฆ version 15.19, in opensuse-leap 16.0

๐Ÿ’… CREATE_STATISTICS ๐Ÿ’…

okay so bestie, welcome to the CREATE STATISTICS page from the PostgreSQL 15.19 Documentation โœจ this is the manual for defining extended statistics and honestly? it's kind of a glow-up for your query planner. let's get into it ๐Ÿซถ


๐ŸŒธ NAME ๐ŸŒธ

CREATE_STATISTICS โ€” define extended statistics ๐Ÿ’–


โœจ SYNOPSIS โœจ

CREATE STATISTICS [ IF NOT EXISTS ] statistics_name
    ON ( expression )
    FROM table_name

CREATE STATISTICS [ IF NOT EXISTS ] statistics_name
    [ ( statistics_kind [, ... ] ) ]
    ON { column_name | ( expression ) }, { column_name | ( expression ) } [, ...]
    FROM table_name

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

so CREATE STATISTICS creates a brand-new extended statistics object that tracks data about the table, foreign table or materialized view you point it at. the object lives in the current database and it's owned by whoever ran the command ๐Ÿ‘‘ (yes, that's you).

this command has two basic forms, and they're giving completely different vibes:

  • form one lets you collect univariate statistics for a single expression. it gives you the same kinda benefits as an expression index but without all the overhead of index maintenance โ€” lowkey iconic. this form does not let you specify the statistics kind, because the various statistics kinds only refer to multivariate statistics.
  • form two lets you collect multivariate statistics on multiple columns and/or expressions, and you can optionally say which statistics kinds to include. this form will also automatically cause univariate statistics to be collected on any expressions in the list ๐Ÿฆ‹

if you give a schema name (like CREATE STATISTICS myschema.mystat ...) then the statistics object gets created in that schema. otherwise it goes in the current schema. and the name of the statistics object has to be distinct from the name of any other statistics object in that same schema โ€” no duplicates, no drama ๐Ÿ’…


๐Ÿ”ฅ PARAMETERS ๐Ÿ”ฅ

  • IF NOT EXISTS โ€” do not throw an error if a statistics object with the same name already exists. a notice gets issued instead. important caveat bestie: 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 you're creating โœจ
  • statistics_kind โ€” a multivariate statistics kind to be computed in this statistics object. currently supported kinds are:
    • ndistinct โ€” enables n-distinct statistics
    • dependencies โ€” enables functional dependency statistics
    • mcv โ€” enables most-common values lists

if this clause is omitted, all supported statistics kinds get included in the statistics object. univariate expression statistics are built automatically if the statistics definition includes any complex expressions rather than just simple column references. for more info, see Section 14.2.2 and Section 75.2 ๐Ÿ“š

  • column_name โ€” the name of a table column to be covered by the computed statistics. this is only allowed when building multivariate statistics. at least two column names or expressions must be specified, and their order is not significant ๐Ÿ™Œ
  • expression โ€” an expression to be covered by the computed statistics. you can use this to build univariate statistics on a single expression, or as part of a list of multiple column names and/or expressions to build multivariate statistics. in that latter case, separate univariate statistics are built automatically for each expression in the list ๐Ÿงš
  • table_name โ€” the name (optionally schema-qualified) of the table containing the column(s) the statistics are computed on; see ANALYZE(7) for an explanation of the handling of inheritance and partitions ๐Ÿ’œ

๐Ÿฆ‹ NOTES ๐Ÿฆ‹

you must be the owner of a table to create a statistics object that reads it. but once it's created, the ownership of the statistics object is independent of the underlying table(s) โ€” they're not codependent, they're just vibing separately ๐Ÿซถ

expression statistics are per-expression and are similar to creating an index on the expression, except they avoid the overhead of index maintenance. expression statistics are built automatically for each expression in the statistics object definition โœจ

ok real talk: extended statistics are not currently used by the planner for selectivity estimations made for table joins. this limitation will likely be removed in a future version of PostgreSQL. so don't come for the planner just yet ๐Ÿ’€

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

๐Ÿ“ example one: functional dependencies

create table t1 with two functionally dependent columns โ€” meaning knowledge of a value in the first column is sufficient for determining the value in the other column. then functional dependency statistics get built 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);

without functional-dependency statistics, the planner would assume the two WHERE conditions are independent, and would multiply their selectivities together to arrive at a much-too-small row count estimate ๐Ÿ˜ญ with such statistics, the planner recognizes that the WHERE conditions are redundant and does not underestimate the row count ๐Ÿ™Œ

๐ŸŒธ example two: MCV lists

create table t2 with two perfectly correlated columns (containing identical data), and an MCV list on those columns:

CREATE TABLE t2 (
    a   int,
    b   int
);

INSERT INTO t2 SELECT mod(i,100), mod(i,100)
                 FROM generate_series(1,1000000) s(i);

CREATE STATISTICS s2 (mcv) ON a, b FROM t2;

ANALYZE t2;

-- valid combination (found in MCV)
EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 1);

-- invalid combination (not found in MCV)
EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 2);

the MCV list gives the planner more detailed information about the specific values that commonly appear in the table, as well as an upper bound on the selectivities of combinations of values that do not appear in the table, allowing it to generate better estimates in both cases ๐Ÿ‘‘

๐Ÿงš example three: expression + ndistinct statistics

create table t3 with a single timestamp column, and run queries using expressions on that column. without extended statistics, the planner has no information about the data distribution for the expressions, and uses default estimates. the planner also does not realize that the value of the date truncated to the month is fully determined by the value of the date truncated to the day ๐Ÿ˜ฎ then expression and ndistinct statistics are built on those two expressions:

CREATE TABLE t3 (
    a   timestamp
);

INSERT INTO t3 SELECT i FROM generate_series('2020-01-01'::timestamp,
                                             '2020-12-31'::timestamp,
                                             '1 minute'::interval) s(i);

ANALYZE t3;

-- the number of matching rows will be drastically underestimated:
EXPLAIN ANALYZE SELECT * FROM t3
  WHERE date_trunc('month', a) = '2020-01-01'::timestamp;

EXPLAIN ANALYZE SELECT * FROM t3
  WHERE date_trunc('day', a) BETWEEN '2020-01-01'::timestamp
                                 AND '2020-06-30'::timestamp;

EXPLAIN ANALYZE SELECT date_trunc('month', a), date_trunc('day', a)
   FROM t3 GROUP BY 1, 2;

-- build ndistinct statistics on the pair of expressions (per-expression
-- statistics are built automatically)
CREATE STATISTICS s3 (ndistinct) ON date_trunc('month', a), date_trunc('day', a) FROM t3;

ANALYZE t3;

-- now the row count estimates are more accurate:
EXPLAIN ANALYZE SELECT * FROM t3
  WHERE date_trunc('month', a) = '2020-01-01'::timestamp;

EXPLAIN ANALYZE SELECT * FROM t3
  WHERE date_trunc('day', a) BETWEEN '2020-01-01'::timestamp
                                 AND '2020-06-30'::timestamp;

EXPLAIN ANALYZE SELECT date_trunc('month', a), date_trunc('day', a)
   FROM t3 GROUP BY 1, 2;

without expression and ndistinct statistics, the planner has no information about the number of distinct values for the expressions, and has to rely on default estimates. the equality and range conditions are assumed to have 0.5% selectivity, and the number of distinct values in the expression is assumed to be the same as for the column (i.e. unique). this results in a significant underestimate of the row count in the first two queries ๐Ÿ˜ญ

moreover, the planner has no information about the relationship between the expressions, so it assumes the two WHERE and GROUP BY conditions are independent, and multiplies their selectivities together to arrive at a severe overestimate of the group count in the aggregate query ๐Ÿ’€ this is further exacerbated by the lack of accurate statistics for the expressions, forcing the planner to use a default ndistinct estimate for the expression derived from ndistinct for the column. with such statistics, the planner recognizes that the conditions are correlated, and arrives at much more accurate estimates โœจ๐Ÿ‘‘


โญ COMPATIBILITY โญ

there is no CREATE STATISTICS command in the SQL standard. we're just built different ๐Ÿ’…


๐Ÿ’– SEE ALSO ๐Ÿ’–


and that's the whole page bestie ๐ŸŽ€ this was PostgreSQL 15.19, dated 2026 โ€” go forth and give your planner the statistics it deserves ๐Ÿฆ‹๐Ÿ’œ stay slay, stay analyzed โœจ

๐Ÿ“ฆ where this page comes from