Skip to content

Commit c1e9cfa

Browse files
committed
Document autoanalyze limitations for partitioned tables
When dealing with partitioned tables, counters for partitioned tables are not updated when modifying child tables. This means autoanalyze may not update optimizer statistics for the parent relations, which can result in poor plans for some queries. It's worth documenting this limitation, so that people are aware of it and can take steps to mitigate it (e.g. by setting up a script executing ANALYZE regularly). Backpatch to v10. Older branches are affected too, of couse, but we no longer maintain those. Author: Justin Pryzby Reviewed-by: Zhihong Yu, Tomas Vondra Backpatch-through: 10 Discussion: https://postgr.es/m/20210913035409.GA10647%40telsasoft.com
1 parent ab80b0f commit c1e9cfa

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

doc/src/sgml/maintenance.sgml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@
289289
to meaningful statistical changes.
290290
</para>
291291

292+
<para>
293+
Tuples changed in partitions and inheritance children do not trigger
294+
analyze on the parent table. If the parent table is empty or rarely
295+
changed, it may never be processed by autovacuum, and the statistics for
296+
the inheritance tree as a whole won't be collected. It is necessary to
297+
run <command>ANALYZE</command> on the parent table manually in order to
298+
keep the statistics up to date.
299+
</para>
300+
292301
<para>
293302
As with vacuuming for space recovery, frequent updates of statistics
294303
are more useful for heavily-updated tables than for seldom-updated
@@ -345,6 +354,19 @@
345354
<command>ANALYZE</> commands on those tables on a suitable schedule.
346355
</para>
347356
</tip>
357+
358+
<tip>
359+
<para>
360+
The autovacuum daemon does not issue <command>ANALYZE</command> commands
361+
for partitioned tables. Inheritance parents will only be analyzed if the
362+
parent itself is changed - changes to child tables do not trigger
363+
autoanalyze on the parent table. If your queries require statistics on
364+
parent tables for proper planning, it is necessary to periodically run
365+
a manual <command>ANALYZE</command> on those tables to keep the statistics
366+
up to date.
367+
</para>
368+
</tip>
369+
348370
</sect2>
349371

350372
<sect2 id="vacuum-for-visibility-map">
@@ -797,6 +819,13 @@ analyze threshold = analyze base threshold + analyze scale factor * number of tu
797819
since the last <command>ANALYZE</command>.
798820
</para>
799821

822+
<para>
823+
Partitioned tables are not processed by autovacuum. Statistics
824+
should be collected by running a manual <command>ANALYZE</command> when it is
825+
first populated, and again whenever the distribution of data in its
826+
partitions changes significantly.
827+
</para>
828+
800829
<para>
801830
Temporary tables cannot be accessed by autovacuum. Therefore,
802831
appropriate vacuum and analyze operations should be performed via

doc/src/sgml/ref/analyze.sgml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,35 @@ ANALYZE [ VERBOSE ] [ <replaceable class="PARAMETER">table_name</replaceable> [
213213
</para>
214214

215215
<para>
216-
If any of the child tables are foreign tables whose foreign data wrappers
217-
do not support <command>ANALYZE</>, those child tables are ignored while
218-
gathering inheritance statistics.
216+
For partitioned tables, <command>ANALYZE</command> gathers statistics by
217+
sampling rows from all partitions; in addition, it will recurse into each
218+
partition and update its statistics. Each leaf partition is analyzed only
219+
once, even with multi-level partitioning. No statistics are collected for
220+
only the parent table (without data from its partitions), because with
221+
partitioning it's guaranteed to be empty.
222+
</para>
223+
224+
<para>
225+
By constrast, if the table being analyzed has inheritance children,
226+
<command>ANALYZE</command> gathers two sets of statistics: one on the rows
227+
of the parent table only, and a second including rows of both the parent
228+
table and all of its children. This second set of statistics is needed when
229+
planning queries that process the inheritance tree as a whole. The child
230+
tables themselves are not individually analyzed in this case.
231+
</para>
232+
233+
<para>
234+
The autovacuum daemon does not process partitioned tables, nor does it
235+
process inheritance parents if only the children are ever modified.
236+
It is usually necessary to periodically run a manual
237+
<command>ANALYZE</command> to keep the statistics of the table hierarchy
238+
up to date.
239+
</para>
240+
241+
<para>
242+
If any child tables or partitions are foreign tables whose foreign
243+
data wrappers do not support <command>ANALYZE</command>, those tables are
244+
ignored while gathering inheritance statistics.
219245
</para>
220246

221247
<para>

0 commit comments

Comments
 (0)