Unnecessary scan from non-overlapping range predicates - Mailing list pgsql-hackers

From Ajit Awekar
Subject Unnecessary scan from non-overlapping range predicates
Date
Msg-id CAER375MzJRka+WEo--jKAoqbP8E2rSrj5Ko7sW9pH8xKDzdf4A@mail.gmail.com
Whole thread Raw
Responses Re: Unnecessary scan from non-overlapping range predicates
List pgsql-hackers
Hi Team,

Please find below an observation regarding query planning  that leads to unnecessary table scan when the WHERE clause contains non-overlapping conditions.


EXPLAIN (costs off)
select * from products where price < 100 AND price > 300;
Seq Scan on products
  Filter: ((price < '100'::numeric) AND (price > '300'::numeric))

Since this condition is false and result will always be empty. Despite this, we still perform unnecessary sequential scan over the table.

Can we  detect such contradictory predicates during planning and optimize them away using a Result node with One-Time Filter: false. This would avoid scanning large tables unnecessarily and improve performance.

Similarly below non-overlapping ranges in query also lead to unnecessary scan without any result.

select * from products where (price between 3 and 100) and (price between 900 and 1000);


Thanks
Ajit

pgsql-hackers by date:

Previous
From: jian he
Date:
Subject: Re: pg_dump misses comments on NOT NULL constraints
Next
From: Jim Jones
Date:
Subject: Re: display hot standby state in psql prompt