'Can postgres tell that some query will return nothing when there are mutually exclusive parts of the where clause?

I'm working on optimizing a database backing an API that accepts timestamps in combinations to form ranges if desired. I've written some code to generate random queries, and have seen some performance degradation for queries that wound up with mutually exclusive times, e.g. `start_time < '2022-03-31' and start_time > '2022-03-31'. A query like this couldn't possibly return any rows (I don't think) and so I was wondering if there is a way for postgres to parse that mutual exclusion and skip scanning anything.

With an index, the performance is fine to discover that there are no rows matching the conditions, but it still does an index scan and condition check. When I have an order by that doesn't match the conditions, then it really slows down. I was wondering if this is something missing from the query planner, or if I'm missing something key.

I tried various combinations of indexes covering all the columns in the order alongside indexes on each individual column involved.

I've searched here and on other sites, but either I couldn't come up with the right keywords or I just missed something.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source