Skip to content

Commit 1b5617e

Browse files
committed
Describe (auto-)analyze behavior for partitioned tables
This explains the new behavior introduced by 0827e8a as well as preexisting. Author: Justin Pryzby <pryzby@telsasoft.com> Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/20210423180152.GA17270@telsasoft.com
1 parent 5eb1b27 commit 1b5617e

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

doc/src/sgml/maintenance.sgml

+6
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ analyze threshold = analyze base threshold + analyze scale factor * number of tu
817817
</programlisting>
818818
is compared to the total number of tuples inserted, updated, or deleted
819819
since the last <command>ANALYZE</command>.
820+
For partitioned tables, inserts, updates and deletes on partitions
821+
are counted towards this threshold; however, DDL
822+
operations such as <literal>ATTACH</literal>, <literal>DETACH</literal>
823+
and <literal>DROP</literal> are not, so running a manual
824+
<command>ANALYZE</command> is recommended if the partition added or
825+
removed contains a statistically significant volume of data.
820826
</para>
821827

822828
<para>

doc/src/sgml/perform.sgml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,8 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
17671767
<para>
17681768
Whenever you have significantly altered the distribution of data
17691769
within a table, running <link linkend="sql-analyze"><command>ANALYZE</command></link> is strongly recommended. This
1770-
includes bulk loading large amounts of data into the table. Running
1770+
includes bulk loading large amounts of data into the table as well as
1771+
attaching, detaching or dropping partitions. Running
17711772
<command>ANALYZE</command> (or <command>VACUUM ANALYZE</command>)
17721773
ensures that the planner has up-to-date statistics about the
17731774
table. With no statistics or obsolete statistics, the planner might

doc/src/sgml/ref/analyze.sgml

+29-11
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,38 @@ ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replacea
250250
</para>
251251

252252
<para>
253-
If the table being analyzed has one or more children,
254-
<command>ANALYZE</command> will gather statistics twice: once on the
255-
rows of the parent table only, and a second time on the rows of the
256-
parent table with all of its children. This second set of statistics
257-
is needed when planning queries that traverse the entire inheritance
258-
tree. The autovacuum daemon, however, will only consider inserts or
259-
updates on the parent table itself when deciding whether to trigger an
260-
automatic analyze for that table. If that table is rarely inserted into
261-
or updated, the inheritance statistics will not be up to date unless you
262-
run <command>ANALYZE</command> manually.
253+
If the table being analyzed is partitioned, <command>ANALYZE</command>
254+
will gather statistics by sampling blocks randomly from its partitions;
255+
in addition, it will recurse into each partition and update its statistics.
256+
(However, in multi-level partitioning scenarios, each leaf partition
257+
will only be analyzed once.)
258+
By constrast, if the table being analyzed has inheritance children,
259+
<command>ANALYZE</command> will gather statistics for it twice:
260+
once on the rows of the parent table only, and a second time on the
261+
rows of the parent table with all of its children. This second set of
262+
statistics is needed when planning queries that traverse the entire
263+
inheritance tree. The child tables themselves are not individually
264+
analyzed in this case.
263265
</para>
264266

265267
<para>
266-
If any of the child tables are foreign tables whose foreign data wrappers
268+
The autovacuum daemon counts inserts, updates and deletes in the
269+
partitions to determine if auto-analyze is needed. However, adding
270+
or removing partitions does not affect autovacuum daemon decisions,
271+
so triggering a manual <command>ANALYZE</command> is recommended
272+
when this occurs.
273+
</para>
274+
275+
<para>
276+
Tuples changed in inheritance children do not count towards analyze
277+
on the parent table. If the parent table is empty or rarely modified,
278+
it may never be processed by autovacuum. It's necessary to
279+
periodically run a manual <command>ANALYZE</command> to keep the
280+
statistics of the table hierarchy up to date.
281+
</para>
282+
283+
<para>
284+
If any of the child tables or partitions are foreign tables whose foreign data wrappers
267285
do not support <command>ANALYZE</command>, those child tables are ignored while
268286
gathering inheritance statistics.
269287
</para>

doc/src/sgml/ref/pg_restore.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,10 @@ CREATE DATABASE foo WITH TEMPLATE template0;
922922

923923
<para>
924924
Once restored, it is wise to run <command>ANALYZE</command> on each
925-
restored table so the optimizer has useful statistics; see
926-
<xref linkend="vacuum-for-statistics"/> and
925+
restored table so the optimizer has useful statistics.
926+
If the table is a partition or an inheritance child, it may also be useful
927+
to analyze the parent to update statistics for the table hierarchy.
928+
See <xref linkend="vacuum-for-statistics"/> and
927929
<xref linkend="autovacuum"/> for more information.
928930
</para>
929931

0 commit comments

Comments
 (0)