public inbox for [email protected]  
help / color / mirror / Atom feed
From: Laurenz Albe <[email protected]>
To: Weck, Luis <[email protected]>
To: [email protected] <[email protected]>
Subject: Re: Constraints elimination during runtime
Date: Wed, 16 Apr 2025 14:38:10 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <IA3PR10MB81133D5DD8051D7018DECD338DBD2@IA3PR10MB8113.namprd10.prod.outlook.com>
References: <IA3PR10MB81133D5DD8051D7018DECD338DBD2@IA3PR10MB8113.namprd10.prod.outlook.com>

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





view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Constraints elimination during runtime
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox