On Thu, Apr 08, 2021 at 04:11:49PM -0400, Alvaro Herrera wrote: > On 2021-Apr-08, Tom Lane wrote: > > > > So I tend to think that my initial instinct was the better direction: we > > > should not be doing any find_all_inheritors() here at all, but instead > > > rely on pg_class.reltuples to be set for the partitioned table. > > > > +1 > > This patch does that.
|commit 0e69f705cc1a3df273b38c9883fb5765991e04fe |Author: Alvaro Herrera <alvhe...@alvh.no-ip.org> |Date: Fri Apr 9 11:29:08 2021 -0400 | | Set pg_class.reltuples for partitioned tables | | 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. + 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); + } This refers to "relpages", but I think it means "reltuples". src/include/commands/vacuum.h:extern void vac_update_relstats(Relation relation, src/include/commands/vacuum.h- BlockNumber num_pages, src/include/commands/vacuum.h- double num_tuples, src/include/commands/vacuum.h- BlockNumber num_all_visible_pages, I'm adding it for the next round of "v14docs" patch if you don't want to make a separate commit for that. -- Justin