1ANALYZE(7) PostgreSQL 9.2.24 Documentation ANALYZE(7)
2
3
4
6 ANALYZE - collect statistics about a database
7
9 ANALYZE [ VERBOSE ] [ table_name [ ( column_name [, ...] ) ] ]
10
12 ANALYZE collects statistics about the contents of tables in the
13 database, and stores the results in the pg_statistic system catalog.
14 Subsequently, the query planner uses these statistics to help determine
15 the most efficient execution plans for queries.
16
17 With no parameter, ANALYZE examines every table in the current
18 database. With a parameter, ANALYZE examines only that table. It is
19 further possible to give a list of column names, in which case only the
20 statistics for those columns are collected.
21
23 VERBOSE
24 Enables display of progress messages.
25
26 table_name
27 The name (possibly schema-qualified) of a specific table to
28 analyze. If omitted, all regular tables (but not foreign tables) in
29 the current database are analyzed.
30
31 column_name
32 The name of a specific column to analyze. Defaults to all columns.
33
35 When VERBOSE is specified, ANALYZE emits progress messages to indicate
36 which table is currently being processed. Various statistics about the
37 tables are printed as well.
38
40 Foreign tables are analyzed only when explicitly selected. Not all
41 foreign data wrappers support ANALYZE. If the table's wrapper does not
42 support ANALYZE, the command prints a warning and does nothing.
43
44 In the default PostgreSQL configuration, the autovacuum daemon (see
45 Section 23.1.6, “The Autovacuum Daemon”, in the documentation) takes
46 care of automatic analyzing of tables when they are first loaded with
47 data, and as they change throughout regular operation. When autovacuum
48 is disabled, it is a good idea to run ANALYZE periodically, or just
49 after making major changes in the contents of a table. Accurate
50 statistics will help the planner to choose the most appropriate query
51 plan, and thereby improve the speed of query processing. A common
52 strategy for read-mostly databases is to run VACUUM(7) and ANALYZE once
53 a day during a low-usage time of day. (This will not be sufficient if
54 there is heavy update activity.)
55
56 ANALYZE requires only a read lock on the target table, so it can run in
57 parallel with other activity on the table.
58
59 The statistics collected by ANALYZE usually include a list of some of
60 the most common values in each column and a histogram showing the
61 approximate data distribution in each column. One or both of these can
62 be omitted if ANALYZE deems them uninteresting (for example, in a
63 unique-key column, there are no common values) or if the column data
64 type does not support the appropriate operators. There is more
65 information about the statistics in Chapter 23, Routine Database
66 Maintenance Tasks, in the documentation.
67
68 For large tables, ANALYZE takes a random sample of the table contents,
69 rather than examining every row. This allows even very large tables to
70 be analyzed in a small amount of time. Note, however, that the
71 statistics are only approximate, and will change slightly each time
72 ANALYZE is run, even if the actual table contents did not change. This
73 might result in small changes in the planner's estimated costs shown by
74 EXPLAIN(7). In rare situations, this non-determinism will cause the
75 planner's choices of query plans to change after ANALYZE is run. To
76 avoid this, raise the amount of statistics collected by ANALYZE, as
77 described below.
78
79 The extent of analysis can be controlled by adjusting the
80 default_statistics_target configuration variable, or on a
81 column-by-column basis by setting the per-column statistics target with
82 ALTER TABLE ... ALTER COLUMN ... SET STATISTICS (see ALTER TABLE
83 (ALTER_TABLE(7))). The target value sets the maximum number of entries
84 in the most-common-value list and the maximum number of bins in the
85 histogram. The default target value is 100, but this can be adjusted up
86 or down to trade off accuracy of planner estimates against the time
87 taken for ANALYZE and the amount of space occupied in pg_statistic. In
88 particular, setting the statistics target to zero disables collection
89 of statistics for that column. It might be useful to do that for
90 columns that are never used as part of the WHERE, GROUP BY, or ORDER BY
91 clauses of queries, since the planner will have no use for statistics
92 on such columns.
93
94 The largest statistics target among the columns being analyzed
95 determines the number of table rows sampled to prepare the statistics.
96 Increasing the target causes a proportional increase in the time and
97 space needed to do ANALYZE.
98
99 One of the values estimated by ANALYZE is the number of distinct values
100 that appear in each column. Because only a subset of the rows are
101 examined, this estimate can sometimes be quite inaccurate, even with
102 the largest possible statistics target. If this inaccuracy leads to bad
103 query plans, a more accurate value can be determined manually and then
104 installed with ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...)
105 (see ALTER TABLE (ALTER_TABLE(7))).
106
107 If the table being analyzed has one or more children, ANALYZE will
108 gather statistics twice: once on the rows of the parent table only, and
109 a second time on the rows of the parent table with all of its children.
110 This second set of statistics is needed when planning queries that
111 traverse the entire inheritance tree. The autovacuum daemon, however,
112 will only consider inserts or updates on the parent table itself when
113 deciding whether to trigger an automatic analyze for that table. If
114 that table is rarely inserted into or updated, the inheritance
115 statistics will not be up to date unless you run ANALYZE manually.
116
117 If the table being analyzed is completely empty, ANALYZE will not
118 record new statistics for that table. Any existing statistics will be
119 retained.
120
122 There is no ANALYZE statement in the SQL standard.
123
125 VACUUM(7), vacuumdb(1), Section 18.4.4, “Cost-based Vacuum Delay”, in
126 the documentation, Section 23.1.6, “The Autovacuum Daemon”, in the
127 documentation
128
129
130
131PostgreSQL 9.2.24 2017-11-06 ANALYZE(7)