Re: Unnecessary scan from non-overlapping range predicates

From: Ajit Awekar <ajitpostgres(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Unnecessary scan from non-overlapping range predicates
Date: 2025-06-26 04:15:31
Message-ID: CAER375NGDA9Mvaw3Vbb-gGH618yo-TD2Z7qmG_PyLSy2MDjNRw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Tom,

Thanks a lot for sharing.

The GUC constraint_exclusion setting is helpful, especially for handling
poorly written queries.

Thanks & Best Regards,
Ajit

On Wed, 25 Jun 2025 at 21:58, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Ajit Awekar <ajitpostgres(at)gmail(dot)com> writes:
> > 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.
>
> This is not done by default because it would be a waste of planner
> cycles for well-written queries. However, if you have a lot of
> poorly-written queries ...
>
> regression=# create table products (price numeric);
> CREATE TABLE
> regression=# explain select * from products where price < 100 AND price >
> 300;
> QUERY PLAN
> -------------------------------------------------------------------
> Seq Scan on products (cost=0.00..30.40 rows=7 width=32)
> Filter: ((price < '100'::numeric) AND (price > '300'::numeric))
> (2 rows)
>
> regression=# set constraint_exclusion to on;
> SET
> regression=# explain select * from products where price < 100 AND price >
> 300;
> QUERY PLAN
> ------------------------------------------
> Result (cost=0.00..0.00 rows=0 width=0)
> One-Time Filter: false
> (2 rows)
>
>
> regards, tom lane
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhou, Zhiguo 2025-06-26 05:07:49 Optimize LWLock scalability via ReadBiasedLWLock for heavily-shared locks
Previous Message Hayato Kuroda (Fujitsu) 2025-06-26 03:46:44 RE: Slot's restart_lsn may point to removed WAL segment after hard restart unexpectedly