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 1mUFVX-0008Mc-6z for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Sep 2021 21:46:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mUFUz-0001d6-JF for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Sep 2021 21:46:21 +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 1mUFUz-0001aN-6a for pgsql-hackers@lists.postgresql.org; Sat, 25 Sep 2021 21:46:21 +0000 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mUFUt-0000zO-7j for pgsql-hackers@postgresql.org; Sat, 25 Sep 2021 21:46:19 +0000 Received: by mail-io1-xd30.google.com with SMTP id i62so1447115ioa.6 for ; Sat, 25 Sep 2021 14:46:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=W/gWWveJws+CneW0YCZ+5HeAfqml/sVcRozhHZxcPFA=; b=X6MzGJe7tYAPLB7JmTqkmGwai2q0UcZUZay8Ckob5iivqk3pared3DrreHaMhZIoHF hjGgQSe+WfB2KqdbP8JUtXmeqvR0hP1eVm1upFRxLYNh/8BHLdLw+Enxem6Slx6e7LWm 07mnOnzREuUssPvL8VnawH39/+B00qQUo2fkBUqxFdgfzyEueZRyDaatsqRy8SEQ1bo1 oQGjUeVdVWIE3iiuEv4JMjAYWgI2B0Yu2Zve54Yj5JIzNBIp2SP1JFznr4nDVEJIYlqF 9++VNfjEQpONuYB1wtb0tD4rst96FKA390RdUXxGGW68VlELNXARhpX1NGFh6L+zYyPm OmRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=W/gWWveJws+CneW0YCZ+5HeAfqml/sVcRozhHZxcPFA=; b=01+/Opp08egM4/wgMzhZKl8GY0uCnLLV1y0mGK19VLCgldTXhDPb9y02ILAMD1/HRL KsfZcCUuRhY9KcpFQXsR6jHnqmbUTgQsceZc9vbfo44fhlbUf0LGa4++RbT9DYYz6GtW EjQl6M+pBpvQY3KT7Q2Q58Wwup+Op1mGinqMTLDLDLds/swGhQIVwYmV5Ae3csZ+1xGQ tGrO7hPWY9qLRwXV4IpBpwzVniXEHPvAEx4ao9cK33PQ863nzXc0u+lZFHDGknf7sSmy yhCQ3OsJn4LYcm6UOrO71CmqwR3FnYRyfgl8d6leKs08BVBhx73MMJDpRZ3J5sIwl1wh A3Ow== X-Gm-Message-State: AOAM530K4oX9OzXdUE0jHjyhUGtCsshW0T6QgFxQcO0E/4ZRiruydOuD CHg13kV6wxQ3BA/sS7eg1tG/Uw== X-Google-Smtp-Source: ABdhPJxHTwGDYPAmQlD2pFpxegfTkmJBM1X2IbFYY/nBtriLfV0TtWoCEWZH0i2kXHT6y1/mO7Od+g== X-Received: by 2002:a5e:8204:: with SMTP id l4mr14454726iom.8.1632606372720; Sat, 25 Sep 2021 14:46:12 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id r7sm6339401ilm.5.2021.09.25.14.46.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Sep 2021 14:46:12 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 03590800A3C; Sat, 25 Sep 2021 16:46:10 -0500 (CDT) Date: Sat, 25 Sep 2021 16:46:10 -0500 From: Justin Pryzby To: Tomas Vondra Cc: pgsql-hackers@postgresql.org, David Rowley , Alvaro Herrera Subject: Re: extended stats on partitioned tables Message-ID: <20210925214610.GW831@telsasoft.com> References: <20210923212624.GI831@telsasoft.com> <20210925195322.GV831@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, Sep 25, 2021 at 11:01:21PM +0200, Tomas Vondra wrote: > > Do you think it's possible to backpatch a fix to handle partitioned tables > > specifically ? > > > > The "tuple already updated" error which I reported and which was fixed by > > 859b3003 involved inheritence children. Since partitioned tables have no data > > themselves, the !inh check could be relaxed. It's not totally clear to me if > > the correct statistics would be used in that case. I suppose the wrong > > (inherited) stats would be wrongly applied affect queries FROM ONLY a > > partitioned table, which seems pointless to write and also hard for the > > estimates to be far off :) > > Hmmm, maybe. To prevent the "tuple concurrently updated" we must ensure we > never build stats with and without inheritance at the same time (for the > same rel). The 859b3003de ensures that by only building extended stats in > the (!inh) case, but we might tweak that based on relkind. See the attached > patch. But I wonder if there are cases that might be hurt by this - that'd > be a regression too, of course. I think we should leave the inheritance case alone, since it hasn't changed in 2 years, and building stats on the table ONLY is a legitimate interpretation, and it's as good as we can do without the catalog change. But the partitioned case used to work, and there's no utility in selecting FROM ONLY a partitioned table, so we might as well build the stats including its partitions. I don't think anything would get worse for the partitioned case. Obviously building inherited ext stats could change plans - that's the point. It's weird that the stats objects which existed for 18 months before being "built" after the patch was applied, but no so weird that the release notes wouldn't be ample documentation. If building statistics caused the plan to change undesirably, the solution would be to drop the stats object, of course. + build_ext_stats = (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) ? inh : (!inh); It's weird to build inherited extended stats for partitioned tables but not for inheritence parents. We could be clever and say "build inherited ext stats for inheritence parents only if we didn't insert any stats for the table itself (because it's empty)". But I think that's fragile: a single tuple in the parent table could cause stats to be built there instead of on its heirarchy, and the extended stats would be used for *both* FROM and FROM ONLY, which is an awful combination. Since do_analyze_rel is only called once for partitioned tables, I think you could write that as: /* Do not build inherited stats (since the catalog cannot support it) except * for partitioned tables, for which numrows==0 and have no non-inherited stats */ build_ext_stats = !inh || onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE; -- Justin