From 0d0e149727d89115803b4528e15f5b3c04bd816b Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Mon, 16 Aug 2021 22:55:06 -0500 Subject: [PATCH] Report last_analyze and analyze_count of partitioned tables.. In v14, partitioned tables are included, but these fields are being reported as zero, which is misleading. --- src/backend/commands/analyze.c | 36 ++++++++++++++++++++++----------- src/backend/postmaster/pgstat.c | 27 +++++++++++++++++-------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 8d7b38d170..0050df08f6 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -626,8 +626,8 @@ do_analyze_rel(Relation onerel, VacuumParams *params, PROGRESS_ANALYZE_PHASE_FINALIZE_ANALYZE); /* - * Update pages/tuples stats in pg_class, and report ANALYZE to the stats - * collector ... but not if we're doing inherited stats. + * Update pages/tuples stats in pg_class ... but not if we're doing + * inherited stats. * * We assume that VACUUM hasn't set pg_class.reltuples already, even * during a VACUUM ANALYZE. Although VACUUM often updates pg_class, @@ -668,20 +668,32 @@ do_analyze_rel(Relation onerel, VacuumParams *params, InvalidMultiXactId, in_outer_xact); } - + } + else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + { /* - * Now report ANALYZE to the stats collector. - * - * 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. + * Partitioned tables don't have storage, so we don't set any fields + * in their pg_class entries except for reltuples, which is necessary + * for auto-analyze to work properly, and relhasindex. */ + 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 || onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) pgstat_report_analyze(onerel, totalrows, totaldeadrows, (va_cols == NIL)); - } /* * If this isn't part of VACUUM ANALYZE, let index AMs do cleanup. diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index a3c35bdf60..2a9673154b 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -1632,21 +1632,31 @@ pgstat_report_analyze(Relation rel, * be double-counted after commit. (This approach also ensures that the * collector ends up with the right numbers if we abort instead of * committing.) + * + * For partitioned tables, we don't report live and dead tuples, because + * such tables don't have any data. */ if (rel->pgstat_info != NULL) { PgStat_TableXactStatus *trans; - for (trans = rel->pgstat_info->trans; trans; trans = trans->upper) + if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + /* If this rel is partitioned, skip modifying */ + livetuples = deadtuples = 0; + else { - livetuples -= trans->tuples_inserted - trans->tuples_deleted; - deadtuples -= trans->tuples_updated + trans->tuples_deleted; + for (trans = rel->pgstat_info->trans; trans; trans = trans->upper) + { + livetuples -= trans->tuples_inserted - trans->tuples_deleted; + deadtuples -= trans->tuples_updated + trans->tuples_deleted; + } + /* count stuff inserted by already-aborted subxacts, too */ + deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples; + /* Since ANALYZE's counts are estimates, we could have underflowed */ + livetuples = Max(livetuples, 0); + deadtuples = Max(deadtuples, 0); } - /* count stuff inserted by already-aborted subxacts, too */ - deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples; - /* Since ANALYZE's counts are estimates, we could have underflowed */ - livetuples = Max(livetuples, 0); - deadtuples = Max(deadtuples, 0); + } pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE); @@ -1999,6 +2009,7 @@ pgstat_initstats(Relation rel) /* We only count stats for things that have storage */ if (!RELKIND_HAS_STORAGE(relkind)) +// relkind != RELKIND_PARTITIONED_TABLE) { rel->pgstat_info = NULL; return; -- 2.17.0