Re: Constraints elimination during runtime - Mailing list pgsql-performance

From Laurenz Albe
Subject Re: Constraints elimination during runtime
Date
Msg-id 3ee9caa4d50df99916922693b987df551a02d991.camel@cybertec.at
Whole thread Raw
In response to Constraints elimination during runtime  ("Weck, Luis" <luis.weck@pismo.io>)
List pgsql-performance
On Wed, 2025-04-16 at 11:16 +0000, Weck, Luis wrote:
> I am not sure if this list is the most appropriate, but I figured I’d share it here…
>
> If a column has a check constraint, such as CHECK (length(value) < 10) or even
> something like a VARCHAR(10) shouldn’t a query like this become a no-op/false instantly?
>
> create table test_constraint (
>      value varchar(10) // could be a CHECK constraint also
>   );
>
> insert into test_constraint values (‘small’);
>
> -- shouldn’t this qual always evaluate to false? 
> select * from test_constraint where value = ‘way too big to fit anyway’;

I am sure that it could be done, but I doubt it would be a good idea.
These extra checks would slow down query planning for most queries a
bit, and only very few queries would benefit from it.

If you are writing a query that uses a user-defined constant, you
could write the query as

  SELECT ...
  FROM test_constraint
  WHERE value = $1
    AND length($1) <= 10;

If the query planner knows the value of the constant, it will evaluate
the second condition when it plans the query and replace it with a
"One-Time Filter: false", which would do exactly what you want.

Yours,
Laurenz Albe



pgsql-performance by date:

Previous
From: "Weck, Luis"
Date:
Subject: Constraints elimination during runtime