Re: pgsql: autovacuum: handle analyze for partitioned tables - Mailing list pgsql-hackers

From Justin Pryzby
Subject Re: pgsql: autovacuum: handle analyze for partitioned tables
Date
Msg-id 20210411015537.GA14564@telsasoft.com
Whole thread Raw
In response to Re: pgsql: autovacuum: handle analyze for partitioned tables  (Alvaro Herrera <alvherre@alvh.no-ip.org>)
List pgsql-hackers
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 <alvherre@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



pgsql-hackers by date:

Previous
From: Dave Cramer
Date:
Subject: Re: PL/R regression on windows, but not linux with master.
Next
From: Zhihong Yu
Date:
Subject: Re: Add header support to text format and matching feature