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 1joRuM-0004Jw-Ec for pgsql-general@arkaria.postgresql.org; Thu, 25 Jun 2020 13:27:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1joRuL-0006lh-5I for pgsql-general@arkaria.postgresql.org; Thu, 25 Jun 2020 13:27:13 +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 1joRuK-0006kD-Rk for pgsql-general@lists.postgresql.org; Thu, 25 Jun 2020 13:27:12 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1joRuI-0002us-1m for pgsql-general@lists.postgresql.org; Thu, 25 Jun 2020 13:27:12 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id EDA0121C45F2 for ; Thu, 25 Jun 2020 16:27:08 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [10.0.1.17] (unknown [188.123.231.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id BAE4221C26E6 for ; Thu, 25 Jun 2020 16:27:08 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1593091628; bh=yt7x+iESEEyLUoNYraz9hW4q9y5aZq7Vjjw8YC8fnv8=; h=Subject:To:References:From:Date:In-Reply-To; b=hhESH3G36hxbUmx5y+LNgs9B7SFDHQlG1yVWvZP2dpOqRByWffjdDL/rHf/joIUnp FhyhgEbbquprIBInqZR7Ika8TkPn8z98kkf6hChxw8OOXMPQZHngkAtu4//QL16pQC C4G0Pk2QSo+M9PVX11UDeH71ejBxO5ajEdEIsPpY= Subject: Re: n_distinct off by a factor of 1000 To: pgsql-general@lists.postgresql.org References: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> From: Pavel Luzanov Message-ID: Date: Thu, 25 Jun 2020 16:27:08 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> Content-Type: multipart/alternative; boundary="------------5960B06FFBF48C35652A38AC" Content-Language: ru-RU List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------5960B06FFBF48C35652A38AC Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hello, > I got my first hint of why this problem occurs when I looked at the > statistics.  For the column in question, "instrument_ref" the > statistics claimed it to be: > > The default_statistics_target=500, and analyze has been run. > select * from pg_stats where attname like 'instr%_ref'; -- Result: > *40.000* > select count(distinct instrumentid_ref) from bigtable -- Result: *33 > 385 922 (!!)* > > That is an astonishing difference of almost a 1000X. > I have tried to increase the statistics target to 5000, and it helps, > but it reduces the error to 100X.  Still crazy high. As far as I know, increasing default_statistics_target will not help. [1] > I have considered these fixes: > - hardcode the statistics to a particular ratio of the total number of > rows You can hardcode the percentage of distinct values: ALTER TABLE bigtable ALTER COLUMN instrument_ref SET ( n_distinct=-0.06 ); /* -1 * (33385922 / 500000000) */ [1] https://www.postgresql.org/message-id/4136ffa0812111823u645b6ec9wdca60b3da4b00499%40mail.gmail.com ----- Pavel Luzanov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --------------5960B06FFBF48C35652A38AC Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
Hello,

I got my first hint of why this problem occurs when I looked at the statistics.  For the column in question, "instrument_ref" the statistics claimed it to be:

The default_statistics_target=500, and analyze has been run.
select * from pg_stats where attname like 'instr%_ref'; -- Result: 40.000
select count(distinct instrumentid_ref) from bigtable -- Result: 33 385 922 (!!)

That is an astonishing difference of almost a 1000X. 

I have tried to increase the statistics target to 5000, and it helps, but it reduces the error to 100X.  Still crazy high.

As far as I know, increasing default_statistics_target will not help. [1]

I have considered these fixes:
- hardcode the statistics to a particular ratio of the total number of rows

You can hardcode the percentage of distinct values:
ALTER TABLE bigtable ALTER COLUMN instrument_ref SET ( n_distinct=-0.06 ); /* -1 * (33385922 / 500000000) */


[1] https://www.postgresql.org/message-id/4136ffa0812111823u645b6ec9wdca60b3da4b00499%40mail.gmail.com
-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

--------------5960B06FFBF48C35652A38AC--