pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Mark Dilger <hornschnorter@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: Re: Using multiple extended statistics for estimates
Date: Thu, 14 Nov 2019 22:45:41 +0100
Message-ID: <20191114214541.3ssauovlhh3vsasr@development> (raw)
In-Reply-To: <10082.1573762564@sss.pgh.pa.us>
References: <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>
	<e41167d8-53cd-0f18-9301-85fd9a70d8d8@gmail.com>
	<20191114200420.c3x2jf7ywzs23uzo@development>
	<10082.1573762564@sss.pgh.pa.us>

On Thu, Nov 14, 2019 at 03:16:04PM -0500, Tom Lane wrote:
>Tomas Vondra <tomas.vondra@2ndquadrant.com> 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

Yeah, and this logic still works - the redundant clauses won't even get
to the selectivity estimation, I think. So maybe the comment is not
quite necessary, because the problem does not even exist ...

Maybe we could do something about the cases that predtest.c can't solve,
but it's not clear if we can be much smarter for types with different
opfamilies.

regards

-- 
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services 





view thread (26+ messages)  latest in thread

Message-ID: <20191114214541.3ssauovlhh3vsasr@development>
Permalink:  ../20191114214541.3ssauovlhh3vsasr@development/
Also on:    postgresql.org/message-id/20191114214541.3ssauovlhh3vsasr@development

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tomas.vondra@2ndquadrant.com, tgl@sss.pgh.pa.us, hornschnorter@gmail.com
  Subject: Re: Using multiple extended statistics for estimates
  In-Reply-To: <20191114214541.3ssauovlhh3vsasr@development>
  References: <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> <e41167d8-53cd-0f18-9301-85fd9a70d8d8@gmail.com> <20191114200420.c3x2jf7ywzs23uzo@development> <10082.1573762564@sss.pgh.pa.us> <20191114214541.3ssauovlhh3vsasr@development>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox