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 1jnjgv-0001X1-JX for pgsql-general@arkaria.postgresql.org; Tue, 23 Jun 2020 14:14:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jnjgu-0004wB-G4 for pgsql-general@arkaria.postgresql.org; Tue, 23 Jun 2020 14:14:24 +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 1jnjgt-0004us-Fj for pgsql-general@lists.postgresql.org; Tue, 23 Jun 2020 14:14:24 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jnjgn-00041w-33 for pgsql-general@lists.postgresql.org; Tue, 23 Jun 2020 14:14:22 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 491F55C0109; Tue, 23 Jun 2020 10:14:16 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 23 Jun 2020 10:14:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=p TY82qW1nlT8/9DzxpcjoF855xdkfVaMXBXsmCKywgo=; b=jyaw94piUPeugCVTm 8/xCXFcoZXNlke51DPUb3RhriKy1fV7cAPoOSXnsVZP74GIoVv+jzA/Tab0VnYJt 30vCgSYYNv2qSGHbRVrU7L+bBv+QI5aoe8+rnt8XVt3XMMbCYdg+/R+LpHuKiLmE Pxs+po9+RA/TMrbnCCVBXTtL6p9T7S/N/p5wm5zoFy+x9CJ4Dpq0HgM8uJRO2/Hh un0jHVwS91dlFN3U4aNjKo8/PRFxrmjAJzNjYRsAS42te4IxGy51KuIh+Rqc/s1y 8UucyZ7EBzlCVgweJ6C6oDVxaeIQjNDTz8Skh4HfFw/2gcjWaZLb8ltTDJ9bFBiv i6AEA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=pTY82qW1nlT8/9DzxpcjoF855xdkfVaMXBXsmCKyw go=; b=OIqGXejh//5eTEir5KXiBrDn9BMIi/2mUnNM9QCOamlLo8lNWRyQwF3Q/ nI1osrDevD+rPQ2kfRuNPc5/PTEj9JOLd95dHKw2q57JEfJKhSSrPGOavnaSs+G7 nWqPQv7Ihh7xpW2C+IYAKzaBQaWB6d8kf903cd4ZHSGvxksOWmc0cgK96LhfeXVl +VDSx7Su0vYTm7BrXc2hEttrMJETpW76qChNalyvVlTEOvvPH+gc+N02jV3cLX6R TPL5/d2nKvD+cDrkn1pAi8mn1aIV+kdSD5Hnsw3RTGGt1xxdV478W3FtZNrRxG/6 xokgaWDPE6OuoTGFjm9ISqFMhCy0w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrudekhedggeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeevveefveefueegffekkefhgeeiueffgfetueejvdfg hfeujeejhfekjeelgedtgeenucfkphepjeehrddujedvrdehrddvfedunecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgr vhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 0646430674F5; Tue, 23 Jun 2020 10:14:14 -0400 (EDT) Subject: Re: n_distinct off by a factor of 1000 To: Fabio Pardi , pgsql-general@lists.postgresql.org References: <6bPRS-MZZcCSPnyGrlbezd8xcvHM0xPQCjDiFH86jAsT4npY3SjXxsis6sUehdrJfHKeP222YgnrGiNgv6pwNC7wowUs3DgEnl5K4Dr7tW4=@protonmail.com> <486df039-f581-411c-c169-6d87ce81bcf6@portavita.eu> From: Adrian Klaver Message-ID: <3c310268-52f2-b960-8efd-bc15b08da7a7@aklaver.com> Date: Tue, 23 Jun 2020 07:14:14 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <486df039-f581-411c-c169-6d87ce81bcf6@portavita.eu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 6/23/20 7:05 AM, Fabio Pardi wrote: > > On 23/06/2020 14:42, Klaudie Willis wrote: >> 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 think you are counting 2 different things here. > > The first query returns all the columns "like 'instr%_ref'" present in > the statistics (so in the whole cluster), while the second is counting > the actual number of different rows in bigtable. I believe the OP actually meant the query to be: select n_distinct from pg_stats where attname like 'instr%_ref'; > > > regards, > > fabio pardi -- Adrian Klaver adrian.klaver@aklaver.com