]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_STATISTICS.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_STATISTICS.7
1 '\" t
2 .\"     Title: CREATE STATISTICS
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE STATISTICS" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_STATISTICS \- define extended statistics
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE STATISTICS [ [ IF NOT EXISTS ] \fIstatistics_name\fR ]
36     ON ( \fIexpression\fR )
37     FROM \fItable_name\fR
38
39 CREATE STATISTICS [ [ IF NOT EXISTS ] \fIstatistics_name\fR ]
40     [ ( \fIstatistics_kind\fR [, \&.\&.\&. ] ) ]
41     ON { \fIcolumn_name\fR | ( \fIexpression\fR ) }, { \fIcolumn_name\fR | ( \fIexpression\fR ) } [, \&.\&.\&.]
42     FROM \fItable_name\fR
43 .fi
44 .SH "DESCRIPTION"
45 .PP
46 \fBCREATE STATISTICS\fR
47 will create a new extended statistics object tracking data about the specified table, foreign table or materialized view\&. The statistics object will be created in the current database and will be owned by the user issuing the command\&.
48 .PP
49 The
50 \fBCREATE STATISTICS\fR
51 command has two basic forms\&. The first form allows univariate statistics for a single expression to be collected, providing benefits similar to an expression index without the overhead of index maintenance\&. This form does not allow the statistics kind to be specified, since the various statistics kinds refer only to multivariate statistics\&. The second form of the command allows multivariate statistics on multiple columns and/or expressions to be collected, optionally specifying which statistics kinds to include\&. This form will also automatically cause univariate statistics to be collected on any expressions included in the list\&.
52 .PP
53 If a schema name is given (for example,
54 CREATE STATISTICS myschema\&.mystat \&.\&.\&.) then the statistics object is created in the specified schema\&. Otherwise it is created in the current schema\&. If given, the name of the statistics object must be distinct from the name of any other statistics object in the same schema\&.
55 .SH "PARAMETERS"
56 .PP
57 IF NOT EXISTS
58 .RS 4
59 Do not throw an error if a statistics object with the same name already exists\&. A notice is issued in this case\&. Note that only the name of the statistics object is considered here, not the details of its definition\&. Statistics name is required when
60 IF NOT EXISTS
61 is specified\&.
62 .RE
63 .PP
64 \fIstatistics_name\fR
65 .RS 4
66 The name (optionally schema\-qualified) of the statistics object to be created\&. If the name is omitted,
67 PostgreSQL
68 chooses a suitable name based on the parent table\*(Aqs name and the defined column name(s) and/or expression(s)\&.
69 .RE
70 .PP
71 \fIstatistics_kind\fR
72 .RS 4
73 A multivariate statistics kind to be computed in this statistics object\&. Currently supported kinds are
74 ndistinct, which enables n\-distinct statistics,
75 dependencies, which enables functional dependency statistics, and
76 mcv
77 which enables most\-common values lists\&. If this clause is omitted, all supported statistics kinds are 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 information, see
78 Section\ \&14.2.2
79 and
80 Section\ \&69.2\&.
81 .RE
82 .PP
83 \fIcolumn_name\fR
84 .RS 4
85 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\&.
86 .RE
87 .PP
88 \fIexpression\fR
89 .RS 4
90 An expression to be covered by the computed statistics\&. This may be used 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 the latter case, separate univariate statistics are built automatically for each expression in the list\&.
91 .RE
92 .PP
93 \fItable_name\fR
94 .RS 4
95 The name (optionally schema\-qualified) of the table containing the column(s) the statistics are computed on; see
96 \fBANALYZE\fR(7)
97 for an explanation of the handling of inheritance and partitions\&.
98 .RE
99 .SH "NOTES"
100 .PP
101 You must be the owner of a table to create a statistics object reading it\&. Once created, however, the ownership of the statistics object is independent of the underlying table(s)\&.
102 .PP
103 Expression statistics are per\-expression and are similar to creating an index on the expression, except that they avoid the overhead of index maintenance\&. Expression statistics are built automatically for each expression in the statistics object definition\&.
104 .PP
105 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
106 PostgreSQL\&.
107 .SH "EXAMPLES"
108 .PP
109 Create table
110 t1
111 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 functional dependency statistics are built on those columns:
112 .sp
113 .if n \{\
114 .RS 4
115 .\}
116 .nf
117 CREATE TABLE t1 (
118     a   int,
119     b   int
120 );
121
122 INSERT INTO t1 SELECT i/100, i/500
123                  FROM generate_series(1,1000000) s(i);
124
125 ANALYZE t1;
126
127 \-\- the number of matching rows will be drastically underestimated:
128 EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);
129
130 CREATE STATISTICS s1 (dependencies) ON a, b FROM t1;
131
132 ANALYZE t1;
133
134 \-\- now the row count estimate is more accurate:
135 EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);
136 .fi
137 .if n \{\
138 .RE
139 .\}
140 .sp
141 Without functional\-dependency statistics, the planner would assume that the two
142 WHERE
143 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
144 WHERE
145 conditions are redundant and does not underestimate the row count\&.
146 .PP
147 Create table
148 t2
149 with two perfectly correlated columns (containing identical data), and an MCV list on those columns:
150 .sp
151 .if n \{\
152 .RS 4
153 .\}
154 .nf
155 CREATE TABLE t2 (
156     a   int,
157     b   int
158 );
159
160 INSERT INTO t2 SELECT mod(i,100), mod(i,100)
161                  FROM generate_series(1,1000000) s(i);
162
163 CREATE STATISTICS s2 (mcv) ON a, b FROM t2;
164
165 ANALYZE t2;
166
167 \-\- valid combination (found in MCV)
168 EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 1);
169
170 \-\- invalid combination (not found in MCV)
171 EXPLAIN ANALYZE SELECT * FROM t2 WHERE (a = 1) AND (b = 2);
172 .fi
173 .if n \{\
174 .RE
175 .\}
176 .sp
177 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\&.
178 .PP
179 Create table
180 t3
181 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:
182 .sp
183 .if n \{\
184 .RS 4
185 .\}
186 .nf
187 CREATE TABLE t3 (
188     a   timestamp
189 );
190
191 INSERT INTO t3 SELECT i FROM generate_series(\*(Aq2020\-01\-01\*(Aq::timestamp,
192                                              \*(Aq2020\-12\-31\*(Aq::timestamp,
193                                              \*(Aq1 minute\*(Aq::interval) s(i);
194
195 ANALYZE t3;
196
197 \-\- the number of matching rows will be drastically underestimated:
198 EXPLAIN ANALYZE SELECT * FROM t3
199   WHERE date_trunc(\*(Aqmonth\*(Aq, a) = \*(Aq2020\-01\-01\*(Aq::timestamp;
200
201 EXPLAIN ANALYZE SELECT * FROM t3
202   WHERE date_trunc(\*(Aqday\*(Aq, a) BETWEEN \*(Aq2020\-01\-01\*(Aq::timestamp
203                                  AND \*(Aq2020\-06\-30\*(Aq::timestamp;
204
205 EXPLAIN ANALYZE SELECT date_trunc(\*(Aqmonth\*(Aq, a), date_trunc(\*(Aqday\*(Aq, a)
206    FROM t3 GROUP BY 1, 2;
207
208 \-\- build ndistinct statistics on the pair of expressions (per\-expression
209 \-\- statistics are built automatically)
210 CREATE STATISTICS s3 (ndistinct) ON date_trunc(\*(Aqmonth\*(Aq, a), date_trunc(\*(Aqday\*(Aq, a) FROM t3;
211
212 ANALYZE t3;
213
214 \-\- now the row count estimates are more accurate:
215 EXPLAIN ANALYZE SELECT * FROM t3
216   WHERE date_trunc(\*(Aqmonth\*(Aq, a) = \*(Aq2020\-01\-01\*(Aq::timestamp;
217
218 EXPLAIN ANALYZE SELECT * FROM t3
219   WHERE date_trunc(\*(Aqday\*(Aq, a) BETWEEN \*(Aq2020\-01\-01\*(Aq::timestamp
220                                  AND \*(Aq2020\-06\-30\*(Aq::timestamp;
221
222 EXPLAIN ANALYZE SELECT date_trunc(\*(Aqmonth\*(Aq, a), date_trunc(\*(Aqday\*(Aq, a)
223    FROM t3 GROUP BY 1, 2;
224 .fi
225 .if n \{\
226 .RE
227 .\}
228 .sp
229 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
230 WHERE
231 and
232 GROUP BY
233 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\&.
234 .SH "COMPATIBILITY"
235 .PP
236 There is no
237 \fBCREATE STATISTICS\fR
238 command in the SQL standard\&.
239 .SH "SEE ALSO"
240 ALTER STATISTICS (\fBALTER_STATISTICS\fR(7)), DROP STATISTICS (\fBDROP_STATISTICS\fR(7))