Re: domain check constraint should also consider domain's collation

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: domain check constraint should also consider domain's collation
Date: 2025-07-14 14:13:40
Message-ID: 610466.1752502420@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

jian he <jian(dot)universality(at)gmail(dot)com> writes:
> CREATE COLLATION case_insensitive (provider = icu, locale =
> '@colStrength=secondary', deterministic = false);
> SELECT 'a' = 'A' COLLATE case_insensitive;
> CREATE DOMAIN d1 as text collate case_insensitive check (value <> 'a');
> SELECT 'A'::d1;

> ``SELECT 'A'::d1`` should error out as domain check constraint not satisfied?

No. In the above, 'value' is of type text, not type d1, and therefore
that comparison will use the default collation. If you try to make it
do something else, you will break far more than you fix. (The
fundamental reason why this is important is that we cannot assume that
the domain constraints hold for the value until after we complete the
CHECK expressions.) So the correct way to create a domain that works
as you have in mind is

CREATE DOMAIN d1 as text collate case_insensitive
check (value <> 'a' COLLATE case_insensitive);

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Burd, Greg 2025-07-14 14:22:17 Re: Changing shared_buffers without restart
Previous Message Aleksander Alekseev 2025-07-14 14:03:44 Re: Missing NULL check after calling ecpg_strdup