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 1svfye-00Go0u-6o for pgsql-bugs@arkaria.postgresql.org; Tue, 01 Oct 2024 16:43:56 +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 1svfyc-0023py-LW for pgsql-bugs@arkaria.postgresql.org; Tue, 01 Oct 2024 16:43:54 +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 1svfyc-0023pp-D0 for pgsql-bugs@lists.postgresql.org; Tue, 01 Oct 2024 16:43:54 +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.94.2) (envelope-from ) id 1svfyU-0022jX-Pt for pgsql-bugs@lists.postgresql.org; Tue, 01 Oct 2024 16:43:53 +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 491Ghkku1860124; Tue, 1 Oct 2024 12:43:46 -0400 From: Tom Lane To: mingwei.tc@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18643: EXPLAIN estimated rows mismatch In-reply-to: <18643-8d455145acd8243e@postgresql.org> References: <18643-8d455145acd8243e@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Tue, 01 Oct 2024 07:47:34 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1860122.1727801026.1@sss.pgh.pa.us> Date: Tue, 01 Oct 2024 12:43:46 -0400 Message-ID: <1860123.1727801026@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > Given predicate A and B, it is expected that size (SELECT X where A) <= > size (SELECT X WHERE A or B) > However, `EXPLAIN SELECT t2.c0 FROM t2 WHERE t2.c0 IN (t2.c0)` returns > rows=2537 I don't see any particular bug here. If you look closely at the EXPLAIN output, you'll see that "t2.c0 IN (t2.c0)" is transformed to "c0 IS NOT NULL" --- but only if it's at top level. So we're estimating selectivities for two quite different conditions in this example. The NOT NULL bit happens because a top-level equality clause is transformed into an "EquivalenceClass", and then when we notice the class has only one member, we prefer to spit out "x IS NOT NULL" rather than "x = x". That has the same effect (at top level of WHERE, anyway) and tends to be estimated more accurately. In any case, in this toy example that lacks an ANALYZE step, the selectivity estimates are mostly going to be garbage. regards, tom lane