Skip to content

Commit 375aed3

Browse files
alvherreYuzuko Hosoyajustinpryzby
committed
Keep stats up to date for partitioned tables
In the long-going saga for analyze on partitioned tables, one thing I missed while reverting 0827e8a is the maintenance of analyze count and last analyze time for partitioned tables. This is a mostly trivial change that enables users assess the need for invoking manual ANALYZE on partitioned tables. This patch, posted by Justin and modified a bit by me (Álvaro), can be mostly traced back to Hosoya-san, though any problems introduced with the scissors are mine. Backpatch to 14, in line with 6f8127b. Co-authored-by: Yuzuko Hosoya <yuzukohosoya@gmail.com> Co-authored-by: Justin Pryzby <pryzby@telsasoft.com> Co-authored-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Reported-by: Justin Pryzby <pryzby@telsasoft.com> Discussion: https://postgr.es/m/20210816222810.GE10479@telsasoft.com
1 parent 1f092a3 commit 375aed3

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/backend/commands/analyze.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,8 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
626626
PROGRESS_ANALYZE_PHASE_FINALIZE_ANALYZE);
627627

628628
/*
629-
* Update pages/tuples stats in pg_class, and report ANALYZE to the stats
630-
* collector ... but not if we're doing inherited stats.
629+
* Update pages/tuples stats in pg_class ... but not if we're doing
630+
* inherited stats.
631631
*
632632
* We assume that VACUUM hasn't set pg_class.reltuples already, even
633633
* during a VACUUM ANALYZE. Although VACUUM often updates pg_class,
@@ -668,20 +668,33 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
668668
InvalidMultiXactId,
669669
in_outer_xact);
670670
}
671-
671+
}
672+
else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
673+
{
672674
/*
673-
* Now report ANALYZE to the stats collector.
674-
*
675-
* We deliberately don't report to the stats collector when doing
676-
* inherited stats, because the stats collector only tracks per-table
677-
* stats.
678-
*
679-
* Reset the changes_since_analyze counter only if we analyzed all
680-
* columns; otherwise, there is still work for auto-analyze to do.
675+
* Partitioned tables don't have storage, so we don't set any fields
676+
* in their pg_class entries except for reltuples and relhasindex.
681677
*/
678+
vac_update_relstats(onerel, -1, totalrows,
679+
0, hasindex, InvalidTransactionId,
680+
InvalidMultiXactId,
681+
in_outer_xact);
682+
}
683+
684+
/*
685+
* Now report ANALYZE to the stats collector. For regular tables, we do
686+
* it only if not doing inherited stats. For partitioned tables, we only
687+
* do it for inherited stats. (We're never called for not-inherited stats
688+
* on partitioned tables anyway.)
689+
*
690+
* Reset the changes_since_analyze counter only if we analyzed all
691+
* columns; otherwise, there is still work for auto-analyze to do.
692+
*/
693+
if (!inh)
682694
pgstat_report_analyze(onerel, totalrows, totaldeadrows,
683695
(va_cols == NIL));
684-
}
696+
else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
697+
pgstat_report_analyze(onerel, 0, 0, (va_cols == NIL));
685698

686699
/*
687700
* If this isn't part of VACUUM ANALYZE, let index AMs do cleanup.

src/backend/postmaster/pgstat.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,11 @@ pgstat_report_analyze(Relation rel,
16321632
* be double-counted after commit. (This approach also ensures that the
16331633
* collector ends up with the right numbers if we abort instead of
16341634
* committing.)
1635+
*
1636+
* Waste no time on partitioned tables, though.
16351637
*/
1636-
if (rel->pgstat_info != NULL)
1638+
if (rel->pgstat_info != NULL &&
1639+
rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
16371640
{
16381641
PgStat_TableXactStatus *trans;
16391642

@@ -1997,8 +2000,10 @@ pgstat_initstats(Relation rel)
19972000
Oid rel_id = rel->rd_id;
19982001
char relkind = rel->rd_rel->relkind;
19992002

2000-
/* We only count stats for things that have storage */
2001-
if (!RELKIND_HAS_STORAGE(relkind))
2003+
/*
2004+
* We only count stats for relations with storage and partitioned tables
2005+
*/
2006+
if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
20022007
{
20032008
rel->pgstat_info = NULL;
20042009
return;

0 commit comments

Comments
 (0)