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 1uzHyY-001oxy-BC for pgsql-performance@arkaria.postgresql.org; Thu, 18 Sep 2025 16:59:18 +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 1uzHyW-000khe-UK for pgsql-performance@arkaria.postgresql.org; Thu, 18 Sep 2025 16:59:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uzHyW-000khV-KN for pgsql-performance@lists.postgresql.org; Thu, 18 Sep 2025 16:59:16 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uzHyU-0017eg-1O for pgsql-performance@lists.postgresql.org; Thu, 18 Sep 2025 16:59:15 +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 58IGxBE11975240; Thu, 18 Sep 2025 12:59:11 -0400 From: Tom Lane To: Jehan-Guillaume de Rorthais cc: =?UTF-8?B?RnLDqWTDqXJpYw==?= Yhuel , "pgsql-performance@lists.postgresql.org" , Christophe Courtois , Laurenz Albe Subject: Re: Indexes on expressions with multiple columns and operators In-reply-to: <20250918172628.44301672@karst> References: <1507576.1758120083@sss.pgh.pa.us> <20250918172628.44301672@karst> Comments: In-reply-to Jehan-Guillaume de Rorthais message dated "Thu, 18 Sep 2025 17:26:28 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1975238.1758214751.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 18 Sep 2025 12:59:11 -0400 Message-ID: <1975239.1758214751@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jehan-Guillaume de Rorthais writes: > On a fresh instance from HEAD with its default configuration, it shows: > Index Scan using foo_s_idx on foo (cost=3D0.29..8.39 rows=3D33333 wid= th=3D13) > Index Cond: (s(crit, ackid) =3D true) > It seems statistics shown in "pg_stats" view for function "s()" are good= . The > query itself even have the same costs than the query using the syntax ti= ps you > provide before. > However, the estimated row number seems wrong in regard with the costs s= hown > and statistics. Yeah. The problem is that clause_selectivity_ext fails to consider use of statistics if the clause looks like "bool_valued_function(...)". If it looks like "bool_valued_function(...) =3D true", that goes down a different code path that does the right thing. Additional factors: * If you just write "WHERE bool_valued_function(...) =3D true", that gets stripped down to "WHERE bool_valued_function(...)" in the name of making equivalent expressions look equivalent. (IS TRUE doesn't get stripped, which is why you have to use that wording to avoid that.) * Index condition building puts back the "=3D true" in order to construct something that satisfies the index AM API. And then it uses that form to get a selectivity estimate for costing purposes --- so the right number goes into the indexscan cost estimate. * But the rowcount estimate is made on the form without "=3D true". That's the number shown in EXPLAIN and used when considering joins. regards, tom lane