Commit e1efc5b4 authored by Alvaro Herrera's avatar Alvaro Herrera

Keep stats up to date for partitioned tables

In the long-going saga for analyze on partitioned tables, one thing I
missed while reverting 0827e8af 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 6f8127b73901.
Co-authored-by: default avatarYuzuko Hosoya <yuzukohosoya@gmail.com>
Co-authored-by: default avatarJustin Pryzby <pryzby@telsasoft.com>
Co-authored-by: default avatarÁlvaro Herrera <alvherre@alvh.no-ip.org>
Reported-by: default avatarJustin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/20210816222810.GE10479@telsasoft.com
parent 359bcf77
...@@ -626,8 +626,8 @@ do_analyze_rel(Relation onerel, VacuumParams *params, ...@@ -626,8 +626,8 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
PROGRESS_ANALYZE_PHASE_FINALIZE_ANALYZE); PROGRESS_ANALYZE_PHASE_FINALIZE_ANALYZE);
/* /*
* Update pages/tuples stats in pg_class, and report ANALYZE to the stats * Update pages/tuples stats in pg_class ... but not if we're doing
* collector ... but not if we're doing inherited stats. * inherited stats.
* *
* We assume that VACUUM hasn't set pg_class.reltuples already, even * We assume that VACUUM hasn't set pg_class.reltuples already, even
* during a VACUUM ANALYZE. Although VACUUM often updates pg_class, * during a VACUUM ANALYZE. Although VACUUM often updates pg_class,
...@@ -668,20 +668,33 @@ do_analyze_rel(Relation onerel, VacuumParams *params, ...@@ -668,20 +668,33 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
InvalidMultiXactId, InvalidMultiXactId,
in_outer_xact); in_outer_xact);
} }
}
else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
{
/* /*
* Now report ANALYZE to the stats collector. * Partitioned tables don't have storage, so we don't set any fields
* * in their pg_class entries except for reltuples and relhasindex.
* We deliberately don't report to the stats collector when doing
* inherited stats, because the stats collector only tracks per-table
* stats.
*
* Reset the changes_since_analyze counter only if we analyzed all
* columns; otherwise, there is still work for auto-analyze to do.
*/ */
vac_update_relstats(onerel, -1, totalrows,
0, hasindex, InvalidTransactionId,
InvalidMultiXactId,
in_outer_xact);
}
/*
* Now report ANALYZE to the stats collector. For regular tables, we do
* it only if not doing inherited stats. For partitioned tables, we only
* do it for inherited stats. (We're never called for not-inherited stats
* on partitioned tables anyway.)
*
* Reset the changes_since_analyze counter only if we analyzed all
* columns; otherwise, there is still work for auto-analyze to do.
*/
if (!inh)
pgstat_report_analyze(onerel, totalrows, totaldeadrows, pgstat_report_analyze(onerel, totalrows, totaldeadrows,
(va_cols == NIL)); (va_cols == NIL));
} else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
pgstat_report_analyze(onerel, 0, 0, (va_cols == NIL));
/* /*
* If this isn't part of VACUUM ANALYZE, let index AMs do cleanup. * If this isn't part of VACUUM ANALYZE, let index AMs do cleanup.
......
...@@ -1618,8 +1618,11 @@ pgstat_report_analyze(Relation rel, ...@@ -1618,8 +1618,11 @@ pgstat_report_analyze(Relation rel,
* be double-counted after commit. (This approach also ensures that the * be double-counted after commit. (This approach also ensures that the
* collector ends up with the right numbers if we abort instead of * collector ends up with the right numbers if we abort instead of
* committing.) * committing.)
*
* Waste no time on partitioned tables, though.
*/ */
if (rel->pgstat_info != NULL) if (rel->pgstat_info != NULL &&
rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
{ {
PgStat_TableXactStatus *trans; PgStat_TableXactStatus *trans;
...@@ -1981,8 +1984,10 @@ pgstat_initstats(Relation rel) ...@@ -1981,8 +1984,10 @@ pgstat_initstats(Relation rel)
Oid rel_id = rel->rd_id; Oid rel_id = rel->rd_id;
char relkind = rel->rd_rel->relkind; char relkind = rel->rd_rel->relkind;
/* We only count stats for things that have storage */ /*
if (!RELKIND_HAS_STORAGE(relkind)) * We only count stats for relations with storage and partitioned tables
*/
if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
{ {
rel->pgstat_info = NULL; rel->pgstat_info = NULL;
return; return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment