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 1mUDjq-0003zC-Hn for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Sep 2021 19:53:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mUDjp-0004Zh-9O for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Sep 2021 19:53:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mUDjo-0004ZY-Sq for pgsql-hackers@lists.postgresql.org; Sat, 25 Sep 2021 19:53:33 +0000 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mUDjh-0005fu-I5 for pgsql-hackers@postgresql.org; Sat, 25 Sep 2021 19:53:31 +0000 Received: by mail-io1-xd2a.google.com with SMTP id b78so11924357iof.2 for ; Sat, 25 Sep 2021 12:53:25 -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=Q9c0D+L8lIBNikPrwMElcAW8huUSgMnxLDefvX6/g20=; b=DTjoV0+t0xvTyCWrXuMdD6rZNXxqZg31cHaf/WLd5+eioDeu1VzZzvCx/cldQW4hTb dg7Iz2Z7DbsUL9YsKvX+Kuhr5qvFXoLhVD560iHoHpUJIZu0e7BDCamkZW6NID5N+SxD NwygH29Prl5/9Q+CJ7vVphtLMw9eDDehFV9MMJYLOKTKOdAHOAPGuRBxmcMXGJsPqWSI PsPuwUC/Qm3WhM+2N3CuBW6fepTQIH6HNhBvZ0dBW6YjvEhGsjXB1VnKsVyJnD8xlorb RGLO1bGkuGYv/UUgTWVlazlff7tknoczEOCEjcmNE7hJlA4VKZe9FxwKPsEnmYW061yD Fb9g== 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=Q9c0D+L8lIBNikPrwMElcAW8huUSgMnxLDefvX6/g20=; b=c0H2anSWpEQ/RkYb6NM2RsJJ7Wnt+gdqnJma9muspWzHplOjHKa6Z3zxlNla8b4fbl 5i2Rd933ChcffwzVDQNjvpkDJLuYRgJJqzQnsipbAUl2YlXScyeYtELEPrdEu5Ykd/dq 7VQTfMTxpJfHxqqAzw+9a0p4635YVWEZ1aU+2ZJS6rb/bNX2YjWpCOgSZ+oPnS0+lnze UmhUMrh+HyiHcKIIsSfegIR/ioIjKtPnCNChrdoMm89uj3u7Res+2snNNyqRHvQsVMcy FUSmbWXblDVupOzQJuwCPdj+o5ZQ6UhTXN4sXsGsjE04/l0DvnlPnV3ZV7adN/hg5JTd Qwyg== X-Gm-Message-State: AOAM533uw5wrbeQTRJWpCq6ulRZ5BUVk23v0HZRCdEmADO9h6VUD9xgB cipTIRNoNPA7XllR6lG7dB7NCQ== X-Google-Smtp-Source: ABdhPJw0Pr1pptwVX8GzRXmfKTMHpWFOrTNvyqKoZ6aHKvpoa3mpoIuS9AXQSnRSaBIm7rQAUvIytg== X-Received: by 2002:a5d:8ad5:: with SMTP id e21mr14277776iot.104.1632599604026; Sat, 25 Sep 2021 12:53:24 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id z6sm6184768iox.28.2021.09.25.12.53.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Sep 2021 12:53:23 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 3759D800B38; Sat, 25 Sep 2021 14:53:22 -0500 (CDT) Date: Sat, 25 Sep 2021 14:53:22 -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: <20210925195322.GV831@telsasoft.com> References: <20210923212624.GI831@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 09:27:10PM +0200, Tomas Vondra wrote: > On 9/23/21 11:26 PM, Justin Pryzby wrote: > > extended stats objects are allowed on partitioned tables since v10. > > https://www.postgresql.org/message-id/flat/CAKJS1f-BmGo410bh5RSPZUvOO0LhmHL2NYmdrC_Jm8pk_FfyCA%40mail.gmail.com > > 8c5cdb7f4f6e1d6a6104cb58ce4f23453891651b > > > > But since 859b3003de they're not populated - pg_statistic_ext(_data) is empty. > > This was the consequence of a commit to avoid an error I reported with stats on > > inheritence parents (not partitioned tables). > > > > preceding 859b3003de, stats on the parent table *did* improve the estimate, > > so this part of the commit message seems to have been wrong? > > |commit 859b3003de87645b62ee07ef245d6c1f1cd0cedb > > | Don't build extended statistics on inheritance trees > > ... > > | Moreover, the current selectivity estimation code only works with individual > > | relations, so building statistics on inheritance trees would be pointless > > | anyway. > > > > |CREATE TABLE p (i int, a int, b int) PARTITION BY RANGE (i); > > |CREATE TABLE pd PARTITION OF p FOR VALUES FROM (1)TO(100); > > |TRUNCATE p; INSERT INTO p SELECT 1, a/100, a/100 FROM generate_series(1,999)a; > > |CREATE STATISTICS pp ON (a),(b) FROM p; > > |VACUUM ANALYZE p; > > |SELECT * FROM pg_statistic_ext WHERE stxrelid ='p'::regclass; > > > > |postgres=# begin; DROP STATISTICS pp; explain analyze SELECT a,b FROM p GROUP BY 1,2; abort; > > | HashAggregate (cost=20.98..21.98 rows=100 width=8) (actual time=1.088..1.093 rows=10 loops=1) > > > > |postgres=# explain analyze SELECT a,b FROM p GROUP BY 1,2; > > | HashAggregate (cost=20.98..21.09 rows=10 width=8) (actual time=1.082..1.086 rows=10 loops=1) > > > > So I think this is a regression, and extended stats should be populated for > > partitioned tables - I had actually done that for some parent tables and hadn't > > noticed that the stats objects no longer do anything. ... > Agreed, that seems like a regression, but I don't see how to fix that > without having the extra flag in the catalog. Otherwise we can store just > one version for each statistics object :-( 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 :) > Attached is a PoC that I quickly bashed together today. It's pretty raw, but > it passed "make check" and I think it does most of the things right. Can you > try if this fixes the estimates with partitioned tables? I think pg_stats_ext_exprs also needs to expose the inherited flag. Thanks, -- Justin