Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVLXL-0008MZ-8C for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 20:16:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVLXK-0005yF-3L for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 20:16:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVLXJ-0005y8-NF for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2019 20:16:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iVLXC-0005KL-UM for pgsql-hackers@postgresql.org; Thu, 14 Nov 2019 20:16:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xAEKG4L0010083; Thu, 14 Nov 2019 15:16:04 -0500 From: Tom Lane To: Tomas Vondra cc: Mark Dilger , PostgreSQL Hackers Subject: Re: Using multiple extended statistics for estimates In-reply-to: <20191114200420.c3x2jf7ywzs23uzo@development> References: <20191028152048.jc6pqv5hb7j77ocp@development> <20191106195440.wtxsyhiwdidl37hv@development> <20191106195849.odhhc66xd23hw6hf@development> <97932e1d-53f1-bad3-efc2-1d98cab70727@gmail.com> <0da76f1f-5f9c-3ce3-3ac6-042d2a18430a@gmail.com> <20191110173428.w5klequcmb6gawaa@development> <20191113152823.jdpertc73gfo2mk7@development> <2e183f8d-1304-6024-90de-1017a861a235@gmail.com> <20191114155541.zliellgi2if4bb5f@development> <20191114200420.c3x2jf7ywzs23uzo@development> Comments: In-reply-to Tomas Vondra message dated "Thu, 14 Nov 2019 21:04:20 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <10081.1573762564.1@sss.pgh.pa.us> Date: Thu, 14 Nov 2019 15:16:04 -0500 Message-ID: <10082.1573762564@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Tomas Vondra writes: > For the case with equal Const values that should be mostly obvious, i.e. > "a=1 AND a=1 AND a=1" has the same selectivity as "a=1". > The case with different Const values is harder, unfortunately. It might > seem obvious that "a=1 AND a=2" means there are no matching rows, but > that heavily relies on the semantics of the equality operator. And we > can't simply compare the Const values either, I'm afraid, because there > are cases with cross-type operators like > a = 1::int AND a = 1.0::numeric > where the Consts are of different type, yet both conditions can be true. FWIW, there's code in predtest.c to handle exactly that, at least for types sharing a btree opfamily. Whether it's worth applying that logic here is unclear, but note that we've had the ability to recognize redundant and contradictory clauses for a long time: regression=# explain select * from tenk1 where two = 1; QUERY PLAN ------------------------------------------------------------ Seq Scan on tenk1 (cost=0.00..470.00 rows=5000 width=244) Filter: (two = 1) (2 rows) regression=# explain select * from tenk1 where two = 1 and two = 1::bigint; QUERY PLAN ------------------------------------------------------------ Seq Scan on tenk1 (cost=0.00..470.00 rows=5000 width=244) Filter: (two = 1) (2 rows) regression=# explain select * from tenk1 where two = 1 and two = 2::bigint; QUERY PLAN --------------------------------------------------------------- Result (cost=0.00..470.00 rows=1 width=244) One-Time Filter: false -> Seq Scan on tenk1 (cost=0.00..470.00 rows=1 width=244) Filter: (two = 1) (4 rows) It falls down on regression=# explain select * from tenk1 where two = 1 and two = 2::numeric; QUERY PLAN ----------------------------------------------------------- Seq Scan on tenk1 (cost=0.00..520.00 rows=25 width=244) Filter: ((two = 1) AND ((two)::numeric = '2'::numeric)) (2 rows) because numeric isn't in the same opfamily, so these clauses can't be compared easily. regards, tom lane