Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xnr53-0002F5-Av for pgsql-general@arkaria.postgresql.org; Mon, 10 Nov 2014 15:40:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xnr52-0007Cl-64 for pgsql-general@arkaria.postgresql.org; Mon, 10 Nov 2014 15:40:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xnr51-0007Cf-6B for pgsql-general@postgresql.org; Mon, 10 Nov 2014 15:40:35 +0000 Received: from smtp1-g21.free.fr ([212.27.42.1]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xnr4t-0007gw-PT for pgsql-general@postgresql.org; Mon, 10 Nov 2014 15:40:33 +0000 Received: from erg (unknown [82.66.247.92]) by smtp1-g21.free.fr (Postfix) with ESMTPS id 6BB389400DB; Mon, 10 Nov 2014 16:38:16 +0100 (CET) Date: Mon, 10 Nov 2014 16:41:11 +0100 From: Jehan-Guillaume de Rorthais To: Tom Lane Cc: "pgsql-general@postgresql.org" Subject: Re: Strange avg value size for index on expression in pg_stats Message-ID: <20141110164111.665de42c@erg> In-Reply-To: <24416.1415630363@sss.pgh.pa.us> References: <20141110115254.2997aaf8@erg> <24416.1415630363@sss.pgh.pa.us> X-Mailer: Claws Mail 3.10.1 (GTK+ 2.24.24; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org On Mon, 10 Nov 2014 09:39:23 -0500 Tom Lane wrote: > Jehan-Guillaume de Rorthais writes: > > While investigating about a wrong result with my btree bloat estimation > > query, I found a strange stat deviation between the average size of a value > > in its table and its average size in one index on the "lower" expression. > > ANALYZE doesn't look at the physical index at all. For an expression > index, it will recompute the index expression at each sampled row, and > then take statistics on those values. In the case you've got here, it's > re-running the lower() function and looking at the uncompressed result. > So that accounts for the discrepancy in average width stats. Ok, understood. But in my demo scenario, I used *only* md5 to populate the test table. So data length is always 32 and the average width length is exact. No possible deviation, even with lower(). To be quite dense: postgres@test=# select length(lower(md5('a'))) = length(md5('b')), length(md5('c')) = length(md5('d')); -[ RECORD 1 ] ?column? | t ?column? | t And here is another test with a static string for all rows: postgres@test=# create table test as select '1234567890123456789012'::text as t from generate_series(1,100); SELECT 100 postgres@test=# create index ON test (lower(t)); CREATE INDEX postgres@test=# analyze test; ANALYZE postgres@test=# select tablename, attname, avg_width from pg_stats where schemaname = 'public'; tablename | attname | avg_width ----------------+---------+----------- test | t | 23 test_lower_idx | lower | 26 AFAIU, we should not have a discrepancy here. > > This tiny difference is the source of a very bad estimation with the > > Btree bloat estimation query when values are around an alignement > > boundary. > > TBH, if that query is relying on ANALYZE width estimates to be accurate > to the last byte, its reliability is going to be horrid anyway. Well, I'm aware of that. I don't need an accuracy to the last byte. This query doesn't even pay attention to the data alignment padding anyway (I measured some 10% deviation in a case because of this). This request only helps guessing the bloat evolution in Btrees or quickly discover *big* deviations. In many situations, we can not afford a call to pgstattuple.avg_leaf_density(). But this statistic difference between two values with the exact same size is itching me. Sa far, I couldn't find a logical explanation and it just looks like a wrong statistic. Regards, -- Jehan-Guillaume (ioguix) de Rorthais -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general