postgresql_16:9.6.10.1 Rationale
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > citext — a case-insensitive character string type > Rationale
The standard approach to doing case-insensitive matches in PostgreSQL has been to use the lower function when comparing values, for example SELECT * FROM tab WHERE lower(col) = LOWER(?);
This works reasonably well, but has a number of drawbacks:
- It makes your SQL statements verbose, and you always have to remember to use
loweron both the column and the query value. - It won't use an index, unless you create a functional index using
lower. - If you declare a column as
UNIQUEorPRIMARY KEY, the implicitly generated index is case-sensitive. So it's useless for case-insensitive searches, and it won't enforce uniqueness case-insensitively.
The citext data type allows you to eliminate calls to lower in SQL queries, and allows a primary key to be case-insensitive. citext is locale-aware, just like text, which means that the matching of upper case and lower case characters is dependent on the rules of the database's LC_CTYPE setting. Again, this behavior is identical to the use of lower in queries. But because it's done transparently by the data type, you don't have to remember to do anything special in your queries.