From 0e69f705cc1a3df273b38c9883fb5765991e04fe Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 9 Apr 2021 11:29:08 -0400 Subject: Set pg_class.reltuples for partitioned tables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When commit 0827e8af70f4 added auto-analyze support for partitioned tables, it included code to obtain reltuples for the partitioned table as a number of catalog accesses to read pg_class.reltuples for each partition. That's not only very inefficient, but also problematic because autovacuum doesn't hold any locks on any of those tables -- and doesn't want to. Replace that code with a read of pg_class.reltuples for the partitioned table, and make sure ANALYZE and TRUNCATE properly maintain that value. I found no code that would be affected by the change of relpages from zero to non-zero for partitioned tables, and no other code that should be maintaining it, but if there is, hopefully it'll be an easy fix. Per buildfarm. Author: Álvaro Herrera Reviewed-by: Zhihong Yu Discussion: https://postgr.es/m/1823909.1617862590@sss.pgh.pa.us --- src/backend/commands/analyze.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/backend/commands/analyze.c') diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 182a1330332..cffcd543029 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -656,6 +656,18 @@ do_analyze_rel(Relation onerel, VacuumParams *params, in_outer_xact); } } + else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) + { + /* + * Partitioned tables don't have storage, so we don't set any fields in + * their pg_class entries except for relpages, which is necessary for + * auto-analyze to work properly. + */ + vac_update_relstats(onerel, -1, totalrows, + 0, false, InvalidTransactionId, + InvalidMultiXactId, + in_outer_xact); + } /* * Now report ANALYZE to the stats collector. For regular tables, we do -- cgit v1.2.3