Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNIWG-00GS21-9B for pgsql-performance@arkaria.postgresql.org; Thu, 05 Jun 2025 21:53:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uNIWA-00Ffoa-Lz for pgsql-performance@arkaria.postgresql.org; Thu, 05 Jun 2025 21:52:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNIWA-00FfoS-CY for pgsql-performance@lists.postgresql.org; Thu, 05 Jun 2025 21:52:59 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uNIW8-000Qnu-2v for pgsql-performance@lists.postgresql.org; Thu, 05 Jun 2025 21:52:58 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 555LqrBL1331734; Thu, 5 Jun 2025 17:52:53 -0400 From: Tom Lane To: Mark Frost cc: "pgsql-performance@lists.postgresql.org" , Alexander Korotkov Subject: Re: Poor row estimates from planner, stat `most_common_elems` sometimes missing for a text[] column In-reply-to: <1311236.1749149830@sss.pgh.pa.us> References: <1311236.1749149830@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 05 Jun 2025 14:57:10 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1331732.1749160373.1@sss.pgh.pa.us> Date: Thu, 05 Jun 2025 17:52:53 -0400 Message-ID: <1331733.1749160373@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I wrote: > The part of that that seems to be going off the rails is > this selection of a cutoff frequency below which element values > will be dropped: > cutoff_freq = 9 * element_no / bucket_width; > The first thing I find suspicious here is that the calculation is > based on element_no (the total number of array elements processed) > and not nonnull_cnt (the maximum possible frequency). Is that > really right? I did some more digging and found that that calculation was introduced (in the older tsvector code) in bc0f08092, which traces to this discussion: https://www.postgresql.org/message-id/flat/4BF4357E.6000505%40krogh.cc So the use of element_no is correct, because what we need to consider here is the total number of values fed to the LC algorithm. Also, my thought that maybe we should reject entries with f < 2 is bogus, because at the end of the algorithm f is not necessarily the true count of occurrences of the value: some early occurrences could have been forgotten via pruning. The "behavioral cliff" is annoying but I'm not sure there is much to be done about it: having a single (still-remembered) occurrence gets less and less significant as the total input size increases, so sooner or later you are going to hit a point where such values should be thrown away. So at this point I'm thinking that there is nothing wrong with ANALYZE's algorithm, although I now see that there are some relevant comments in ts_typanalyze.c that probably ought to be transposed into array_typanalyze.c. The idea of treating lack of MCELEM differently from complete lack of stats still seems to have merit, though. regards, tom lane