Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lTDMc-0008Uu-1W for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Apr 2021 00:45:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lTDMa-0003ts-Lc for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Apr 2021 00:45:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lTDMa-0003tl-Eh for pgsql-hackers@lists.postgresql.org; Mon, 05 Apr 2021 00:45:08 +0000 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lTDMW-00051x-Rx for pgsql-hackers@lists.postgresql.org; Mon, 05 Apr 2021 00:45:07 +0000 Received: by mail-ej1-x629.google.com with SMTP id u21so14745400ejo.13 for ; Sun, 04 Apr 2021 17:45:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=OsPsab4/Su4hlFLvNSOAOSDQEUpv54d7HA63aI3te3Q=; b=WBM3UE7WBw46aumPy6N1anlJ9TvQ8tUb+O+57Jxc3wuCjYWziwAT98sl4+ULY6+/Ri Fs0HUJVUWi9EmynWSR7iZKqUIbRjz8BgYWJGveP61N46SX6DgrUXNpr2Xty4PXVq7FU3 2Y3ryknaHXaK/NM0zGCuMusCrw0Z0eEHY+Xu0cEYpButBRKMhhFr8UHj7lR+2lzIEKxV QuAtsDBcUjxWQRtU/srNJBr0SKGFrPeMjE7L+6NKg48gMbIn804O1dmiZ9xFpmLgFwgo H2gJVfC6lzv+53wZ6v6vUG4MwrLd2y7mhfy9TWZLKEYn06T20S4IETXFbsAdKeytJZF8 yvfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=OsPsab4/Su4hlFLvNSOAOSDQEUpv54d7HA63aI3te3Q=; b=ps6IKa4WpkyKXYglwYBtenySjM263LN6oDIRzxy2Pp57A0/aivngTVhp2AIA0b+jPm RTZMP6WpyPwn7vNnm6UfTv96sjx2dgMPF3iuPeQuEDx1ovOlCsouyUD7ceakgVY9NqKX SCRj4rokhUJrA29l2bQKlPPzjlq0sueKe28FChITo0aIu1guw0xdXNIryUhrlO6k1yeR uvUXBd4RUt7ooLLZ3Wm+m812/ekkFUy05ZHY427H3Zxccvr4+wQuMosWuUrJDXl3sAjT ISR2+uuH7PChXx0cezdfOSkdt64O9zLUzHmL+/T4IuUghM0fGMh0Aeal1am/B8/UzVzc AWBQ== X-Gm-Message-State: AOAM533bUbKulhrPUCDHFsqhHSneiMoAO723c4GQj8jwoiGZb1meTY8u HvHg+N1WHdqOy8gv9WrcmwlgINpQ3ggleMaSrI2+7bSr4i8XQ1irT5SkDSuSC/lyQst+ZE2D4rn qk5SQCur1pxwZKrqvX2rGqPYzhmuLDhPRzNHYTRIc7KeuYNgtiZ7LQXIUUFdBpjyzh+wqK0t2BI WMcUYnBHqdFvLnaf6SLVIfrh3vZYtd5QJwK6xpptvyWqPmCnPsC9XK X-Google-Smtp-Source: ABdhPJzT98+ynO8TTuxva+QSovA3mXAPKYP/5QI9xp6mQWpWuQ9teXeJKe9P5rcNNC2ncDnmaDHYGA== X-Received: by 2002:a17:906:fccd:: with SMTP id qx13mr8183488ejb.275.1617583502859; Sun, 04 Apr 2021 17:45:02 -0700 (PDT) Received: from [10.137.0.20] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id s20sm7555936ejj.38.2021.04.04.17.45.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 04 Apr 2021 17:45:02 -0700 (PDT) Subject: Re: Autovacuum on partitioned table (autoanalyze) From: Tomas Vondra To: Alvaro Herrera , yuzuko Cc: David Steele , Kyotaro Horiguchi , Justin Pryzby , Daniel Gustafsson , Amit Langote , Masahiko Sawada , Laurenz Albe , PostgreSQL Hackers , Greg Stark References: <20210403194234.GA18917@alvherre.pgsql> <73dc10a0-cc6a-5d12-7f0f-e7410fa76d9c@enterprisedb.com> Message-ID: <7f27445b-95cf-24be-fe95-a6f66b7db1b9@enterprisedb.com> Date: Mon, 5 Apr 2021 02:44:45 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <73dc10a0-cc6a-5d12-7f0f-e7410fa76d9c@enterprisedb.com> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/4/21 9:08 PM, Tomas Vondra wrote: > On 4/3/21 9:42 PM, Alvaro Herrera wrote: >> Thanks for the quick rework. I like this design much better and I think >> this is pretty close to committable. Here's a rebased copy with some >> small cleanups (most notably, avoid calling pgstat_propagate_changes >> when the partition doesn't have a tabstat entry; also, free the lists >> that are allocated in a couple of places). >> >> I didn't actually verify that it works. >>> ... > > 3) pgstat_recv_analyze > > Shouldn't it propagate the counters before resetting them? I understand > that for the just-analyzed relation we can't do better, but why not to > propagate the counters to parents? (Not necessarily from this place in > the stat collector, maybe the analyze process should do that.) > FWIW the scenario I had in mind is something like this: create table t (a int, b int) partition by hash (a); create table p0 partition of t for values with (modulus 2, remainder 0); create table p1 partition of t for values with (modulus 2, remainder 1); insert into t select i, i from generate_series(1,1000000) s(i); select relname, n_mod_since_analyze from pg_stat_user_tables; test=# select relname, n_mod_since_analyze from pg_stat_user_tables; relname | n_mod_since_analyze ---------+--------------------- t | 0 p0 | 499375 p1 | 500625 (3 rows) test=# analyze p0, p1; ANALYZE test=# select relname, n_mod_since_analyze from pg_stat_user_tables; relname | n_mod_since_analyze ---------+--------------------- t | 0 p0 | 0 p1 | 0 (3 rows) This may seem a bit silly - who would analyze the hash partitions directly? However, with other partitioning schemes (list, range) it's quite plausible that people load data directly into partition. They can analyze the parent explicitly too, but with multi-level partitioning that probably requires analyzing all the ancestors. The other possible scenario is about rows inserted while p0/p1 are being processed by autoanalyze. That may actually take quite a bit of time, depending on vacuum cost limit. So I still think we should propagate the delta after the analyze, before we reset the counters. I also realized relation_needs_vacanalyze is not really doing what I suggested - it propagates the counts, but does so in the existing loop which checks which relations need vacuum/analyze. That means we may skip the parent table in the *current* round, because it'll see the old (not yet updated) counts. It's likely to be processed in the next autovacuum round, but that may actually not happen. The trouble is the reltuples for the parent is calculated using *current* child reltuples values, but we're comparing it to the *old* value of changes_since_analyze. So e.g. if enough rows were inserted into the partitions, it may still be below the analyze threshold. What I proposed is adding a separate loop that *only* propagates the counts, and then re-read the current stats (perhaps only if we actually propagated anything). And then decide which relations need analyze. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company