Unnecessary scan from non-overlapping range predicates

From: Ajit Awekar <ajitpostgres(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Unnecessary scan from non-overlapping range predicates
Date: 2025-06-25 15:46:38
Message-ID: CAER375MzJRka+WEo--jKAoqbP8E2rSrj5Ko7sW9pH8xKDzdf4A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jim Jones 2025-06-25 15:50:39 Re: display hot standby state in psql prompt
Previous Message Álvaro Herrera 2025-06-25 15:45:06 Re: pg_dump misses comments on NOT NULL constraints